EzDevInfo.com

tkinter interview questions

Top tkinter frequently asked interview questions

How can I create a directly-executable cross-platform GUI app using Python?

Python works on multiple platforms and can be used for desktop and web applications, thus I conclude that there is some way to compile it into an executable for Mac, Windows and Linux.

The problem being I have no idea where to start or how to write a GUI with it, can anybody shed some light on this and point me in the right direction please?


Source: (StackOverflow)

Install tkinter for Python

I am trying to import Tkinter. However, I get an error stating that Tkinter has not been installed:

ImportError: No module named _tkinter, please install the python-tk package

I could probably install it using synaptic manager (can I?), however, I would have to install it on every machine I program on. Would it be possible to add the Tkinter library into my workspace and reference it from there?


Source: (StackOverflow)

Advertisements

Fresh tutorial on tkinter and ttk for Python 3 [closed]

Where can I find the most modern tutorial that teaches tkinter together with ttk?

Tkinter seems the only way to go in Python 3 (don't suggest Python 2), and ttk gave me hope for good-looking GUI.


Source: (StackOverflow)

How do you run your own code alongside Tkinter's event loop?

My little brother is just getting into programming, and for his Science Fair project, he's doing a simulation of a flock of birds in the sky. He's gotten most of his code written, and it works nicely, but the birds need to move every moment.

Tkinter, however, hogs the time for its own event loop, and so his code won't run. Doing root.mainloop() runs, runs, and keeps running, and the only thing it runs is the event handlers.

Is there a way to have his code run alongside the mainloop (without multithreading, it's confusing and this should be kept simple), and if so, what is it?

Right now, he came up with an ugly hack, tying his move() function to <b1-motion>, so that as long as he holds the button down and wiggles the mouse, it works. But there's got to be a better way.


Source: (StackOverflow)

How to stop Tkinter Frame from shrinking to fit its contents?

This is the code that's giving me trouble.

f = Frame(root, width=1000, bg="blue")
f.pack(fill=X, expand=True)

l = Label(f, text="hi", width=10, bg="red", fg="white")
l.pack()

If I comment out the lines with the Label, the Frame displays with the right width. However, adding the Label seems to shrink the Frame down to the Label's size. Is there a way to prevent that from happening?


Source: (StackOverflow)

Tkinter OpenGL context in Python

I need to create an OpenGL context in Tkinker, for using it with PyOpenGL Python module.

Tkinker doesn't natively support OpenGL context, but I found this page on PyOpenGL docs, explaining how to use a wrapper included in the module for this: http://pyopengl.sourceforge.net/documentation/context/

I tried to run the provided code but I got a message saying TOGL module was not found. I downloaded the module from http://togl.sourceforge.net/, but couldn't get it to work.

PS. I did the test on Mac OS X, with Python 3.2, using virtualenv.


Source: (StackOverflow)

How to create a timer using tkinter?

I need to code a program with Python's tkinter library.

My major problem is that I don't know how to create a timer or a clock like hh:mm:ss.

I need it to update itself (that's what I don't know how to do).


Source: (StackOverflow)

Overriding Tkinter "X" button control (the button that close the window) [duplicate]

This question already has an answer here:

When the user presses a close Button that I created, some tasks are performed before exiting. However, if the user clicks on the [X] button in the top-right of the window to close the window, I cannot perform these tasks.

How can I override what happens when the user clicks [X] button?


Source: (StackOverflow)

How to change a widget's font style without knowing the widget's font family/size?

Is there a way to change a Tkinter widget's font style without knowing the widget's font family and font size?

Use case: We create our UI using standard Tkinter widgets (Label, Entry, Text, etc). While our application runs we may want to dynamically change the font style of these widgets to bold and/or italics using the .config() method. Unfortunately there appears to be no way to specify a font spec without specifying the font's family and size.

The following are examples of what we'd like to do, but neither of these examples work:

widget.config(font='bold')

or

widget.config(font=( None, None, 'bold' ))

Source: (StackOverflow)

Adding a scrollbar to a group of widgets in Tkinter

I am using Python to parse entries from a log file, and display the entry contents using Tkinter and so far it's been excellent. The output is a grid of label widgets, but sometimes there are more rows than can be displayed on the screen. I'd like to add a scrollbar, which looks like it should be very easy, but I can't figure it out.

The documentation implies that only the List, Textbox, Canvas and Entry widgets support the scrollbar interface. None of these appear to be suitable for displaying a grid of widgets. It's possible to put arbitrary widgets in a Canvas widget, but you appear to have to use absolute co-ordinates, so I wouldn't be able to use the grid layout manager?

I've tried putting the widget grid into a Frame, but that doesn't seem to support the scrollbar interface, so this doesn't work:

mainframe = Frame(root, yscrollcommand=scrollbar.set)

Can anyone suggest a way round this limitation? I'd hate to have to rewrite in PyQt and increase my executable image size by so much, just to add a scrollbar!


Source: (StackOverflow)

When do I need to call mainloop in a Tkinter application?

Every tkinter tutorial I have seen claims that tkinter.mainloop must be called for windows to be drawn and events to be processed, and they always call this function, even in hello world programs. However, when I try these out in the interactive shell, windows are drawn correctly without having to call mainloop. This example of embedding matplotlib graphics in tkinter produces a relatively complex application, with buttons for panning, zooming and resizing a plot within a tkinter window, and again, this all works if you remove the call to mainloop and run the code in the interactive shell. Of course, if I run the script (with mainloop removed) outside the interactive shell, the program ends too quickly to see what happens, but if I add a call to input to hold the program open everything works correctly (I'm running python 3.2.2 on linux).

So what exactly does mainloop do, and when is it necessary to call it?

EDIT: To clarify, if I open up the GNOME terminal and type

$python3
>>> import tkinter
>>> root = tkinter.Tk()

a window immediately appears without having to call mainloop, and more complex tkinter functionality seems to work as well (for example, adding buttons to the window). In IDLE, a call to mainloop is necessary. It was my understanding that nothing should be drawn, and no events should be processed, until mainloop is called.


Source: (StackOverflow)

Is Tkinter worth learning? [closed]

I generally make my desktop interfaces with Qt, but some recent TK screenshots convince me Tk isn't just ugly motif any more. Additionally Tkinter comes bundled with Python, which makes distribution easier.

So is it worth learning or should I stick with Qt?

example tk interface


Source: (StackOverflow)

How to center a window on the screen in Tkinter?

I'm trying to center a tkinter window. I know I can programatically get the size of the window and the size of the screen and use that to set the geometry, but I'm wondering if there's a simpler way to center the window on the screen.


Source: (StackOverflow)

How do I get rid of Python Tkinter root window?

Do you know a smart way to hide or in any other way get rid of the root window that appears, opened by Tk()? I would like just to use a normal dialog.

Should I skip the dialog and put all my components in the root window? Is it possible or desirable? Or is there a smarter solution?


Source: (StackOverflow)

Tkinter: "Python may not be configured for Tk"

Today I wanted to start working with Tkinter, but I have some problems.

Python 3.2 (r32:88445, Mar 28 2011, 04:14:07) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from tkinter import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.2/tkinter/__init__.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

So how can I configure my Python 3.2 to work with Tkinter?


Source: (StackOverflow)