Questions tagged [tkinter]

Tkinter is the standard Python interface to the "tcl/tk" graphical user interface toolkit. In Python 3, the name of the module changed from Tkinter to tkinter.

Tkinter is the standard Python interface to the tk/tcl graphical user interface (GUI) toolkit. Tkinter runs on most operating systems, making it easy to create rich, cross-platform desktop applications with Python.

, the toolkit upon which Tkinter is based, is a mature, robust toolkit that has its roots in the language, but which can be used with many modern languages including , , and others. Tk provides a native look and feels in most operating systems. For more information about the cross-platform and cross-language features of tk visit tkdocs.com.


Icon

enter image description here


Install tkinter for Python

  • for Linux machines

apt-get install python-tk

Works on Debian-derived distributions like for Ubuntu; refer to your package manager and package list on other distributions.

  • for Windows machines

Tkinter (and, since Python 3.1, ttk) are included with all standard Python distributions. It is important that you use a version of Python supporting Tk 8.5 or greater, and ttk. We recommend installing the "ActivePython" distribution from ActiveState, which includes everything you'll need.

In your web browser, go to Activestate.com, and follow along the links to download the Community Edition of ActivePython for Windows. Make sure you're downloading a 3.1 or newer version, not a 2.x version.

Run the installer, and follow along. You'll end up with a fresh install of ActivePython, located in, e.g. C:\python32. From a Windows command prompt or the Start Menu's "Run..." command, you should then be able to run a Python shell via:

% C:\python32\python

This should give you the Python command prompt. From the prompt, enter these two commands:

>>> import tkinter
>>> tkinter._test()

This should pop up a small window; the first line at the top of the window should say "This is Tcl/Tk version 8.5"; make sure it is not 8.4!

  • for MacOS machines:

If you are using a Python from any current python.org Python installer for macOS (3.8.0+, 3.7.2+, 3.6.8, or 2.7.16+), no further action is needed to use IDLE or Tkinter. A built-in version of Tcl/Tk 8.6 will be used.

If you are using macOS 10.6 or later, the Apple-supplied Tcl/Tk 8.5 has serious bugs that can cause application crashes. If you wish to use IDLE or Tkinter, do not use the Apple-supplied Pythons. Instead, install and use a newer version of Python from python.org or a third-party distributor that supplies or links with a newer version of Tcl/Tk.

More information could refer to IDLE and tkinter with Tcl/Tk on macOS


The following is a Python 3 sample program loosely based on the documentation example but modified to avoid a global import. In Python 2 it is required to change the import statement to import Tkinter as tk with a capital T.

try:
    import tkinter as tk  # For Python3
except ImportError:
    import Tkinter as tk  # For Python2

class App(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.master = master
        self.quit = tk.Button(self, text="QUIT", command=self._destroy, fg="red")
        self.quit.pack(side="left")

        self.hello = tk.Button(self, text="Hello", command=self.say)
        self.hello.pack(side="left")

    def say(self, something="Hello World!"):
        print(something)

    def _destroy(self):
        self.master.destroy()

root = tk.Tk()
app = App(root)
app.pack(side="top", fill="both", expand=True)
app.mainloop()

Due to its power and maturity, tools such as EasyGUI (no longer maintained) have been written on top of Tkinter to make it even easier to use.

Tkinter also has powerful, built-in dialogues that can be used for easily creating native-looking, interactive dialogues. The dialogues can be accessed by import tkinter.simpledialog for Python 3 and import tkSimpleDialog for Python 2.

Related Tags :

  • - A variant on which focuses on separating widget functionality from aesthetics

References :

51180 questions
54
votes
7 answers

How do I create a date picker in tkinter?

Is there any standard way tkinter apps allow the user to choose a date?
MKaras
  • 2,063
  • 2
  • 20
  • 35
54
votes
9 answers

RuntimeError: main thread is not in main loop

When I call self.client = ThreadedClient() in my Python program, I get the error "RuntimeError: main thread is not in main loop" I have already done some googling, but I am making an error somehow ... Can someone please help me out? Full…
user2040823
  • 549
  • 1
  • 4
  • 4
53
votes
9 answers

TypeError: 'builtin_function_or_method' object is not subscriptable

elif(listb[0] == "-test"): run_all.set("testview") listb.pop[0] ERROR: Exception in Tkinter callback Traceback (most recent call last): File "/tools/python/2.7.2/lib/python2.7/lib-tk/Tkinter.py", line 1410, in call return…
Ani
  • 918
  • 1
  • 10
  • 25
53
votes
1 answer

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…
Johnston
  • 707
  • 2
  • 6
  • 8
52
votes
3 answers

How to get tkinter canvas to dynamically resize to window width?

I need to get a canvas in tkinter to set its width to the width of the window, and then dynamically re-size the canvas when the user makes the window smaller/bigger. Is there any way of doing this (easily)?
Annonymous
  • 968
  • 1
  • 9
  • 22
52
votes
9 answers

tkinter python maximize window

I want to initialize a window as maximized, but I can't find out how to do it. I'm using python 3.3 and Tkinter 8.6 on windows 7. I guess the answer is just here: http://www.tcl.tk/man/tcl/TkCmd/wm.htm#m8 but I have no idea how to input it into my…
Rasmus Damgaard Nielsen
  • 1,940
  • 4
  • 18
  • 33
52
votes
11 answers

How to change the foreground or background colour of a Tkinter Button on Mac OS X?

I've been working through the Tkinter chapters in Programming Python and encountered a problem where the foreground and background colours of a button will not change. I am working on a Mac OS X 10.6 system with Python 2.6.1. The colours of a label…
Anthony Cramp
  • 4,495
  • 6
  • 26
  • 27
51
votes
3 answers

How do I remove the light grey border around my Canvas widget?

I've been messing with the Tkinter Canvas widget in order to see if I could make some aesthetically pleasing widgets, and I have a few questions. First, why is there a light grey border around my Canvas widget, and how do I get rid of it? Secondly,…
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
49
votes
4 answers

filedialog, tkinter and opening files

I'm working for the first time on coding a Browse button for a program in Python3. I've been searching the internet and this site, and even python standard library. I have found sample code and very superficial explanations of things, but I haven't…
Icsilk
  • 567
  • 1
  • 5
  • 9
49
votes
15 answers

Why is there no tkinter distribution found?

I am having a problem during the installation of tkinter. I have version 2.7.11. I entered the pip install tkinter on dos but it shows the following message: collecting tkinter Could not find a version that satisfies the requirement tkinter (from…
Hissaan Ali
  • 2,229
  • 4
  • 25
  • 51
48
votes
6 answers

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…
James
  • 3,191
  • 1
  • 23
  • 39
48
votes
3 answers

Hiding Axis Labels

I'm trying to hide the axis labels on the first subplot at 211. I'd like to label the figure, not just a subplot (reference: "Isub Event Characteristics"). How can I control font properties like size, font, color? f = Figure() vdsvgsPlot =…
thenickname
  • 6,684
  • 14
  • 41
  • 42
48
votes
1 answer

How to make a Tkinter window not resizable?

I need a Python script that uses the Tkinter module to create a static (not resizable) window. I have a pretty simple Tkinter script but I don't want it to be resizable. How do I prevent a Tkinter window from being resizable? I honestly don't know…
Squash
  • 955
  • 2
  • 7
  • 17
47
votes
2 answers

Which tkinter modules were renamed in Python 3?

I am trying to create a file chooser dialog box. However, when I try to import tkMessageBox in Python 3, I get an error claiming that the module does not exist. import tkMessageBox # ImportError: No module named 'tkMessageBox' I get similar errors…
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
47
votes
1 answer

Tkinter tkFileDialog doesn't exist

I'm trying to show a open file dialog using Tkinter in Python. Every example I find seems very easy to use, but they all start with the line: import tkFileDialog This line throws an error for me, saying No module named 'tkFileDialog' It seems my…
gfrung4
  • 1,658
  • 3
  • 14
  • 22