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
128
votes
4 answers

Switch between two frames in tkinter?

I have built my first few scripts with a nice little GUI on them, as the tutorials have shown me, but none of them address what to do for a more complex program. If you have something with a 'start menu', for your opening screen, and upon user…
Max Tilley
  • 1,509
  • 3
  • 10
  • 9
125
votes
17 answers

How do I close a tkinter window?

How do I end a Tkinter program? Let's say I have this code: from Tkinter import * def quit(): # code to exit root = Tk() Button(root, text="Quit", command=quit).pack() root.mainloop() How should I define the quit function to exit my…
Matt Gregory
  • 8,074
  • 8
  • 33
  • 40
124
votes
4 answers

Understanding lambda in Python and using it to pass multiple arguments

After reading everything I can find on lambda expressions in Python, I still don't understand how to make it do what I want. Everyone uses the example: lambda x, y : x + y Why do you need to state both x and y before the :? Also how do you make it…
Talisin
  • 2,370
  • 3
  • 18
  • 17
115
votes
3 answers

Adding padding to a tkinter widget only on one side

How can I add padding to a tkinter window, without tkinter centering the widget? I tried: self.canvas_l = Label(self.master, text="choose a color:", font="helvetica 12") self.canvas_l.grid(row=9, column=1, sticky=S, ipady=30) and self.canvas_l =…
Jack S.
  • 2,501
  • 6
  • 25
  • 27
109
votes
10 answers

Interactively validating Entry widget content in tkinter

What is the recommended technique for interactively validating content in a tkinter Entry widget? I've read the posts about using validate=True and validatecommand=command, and it appears that these features are limited by the fact that they get…
Malcolm
  • 5,125
  • 10
  • 52
  • 75
107
votes
18 answers

PermissionError: [Errno 13] Permission denied

I'm getting this error : Exception in Tkinter callback Traceback (most recent call last): File "C:\Python34\lib\tkinter\__init__.py", line 1538, in __call__ return self.func(*args) File…
Marc Schmitt
  • 1,299
  • 3
  • 10
  • 7
104
votes
5 answers

Why is my Button's command executed immediately when I create the Button, and not when I click it?

My code is: from Tkinter import * admin = Tk() def button(an): print(an) print('het') b = Button(admin, text='as', command=button('hey')) b.pack() mainloop() The button doesn't work, it prints 'hey' and 'het' once without my command, and…
salk
  • 1,051
  • 2
  • 8
  • 4
104
votes
3 answers

Tkinter understanding mainloop

Till now, I used to end my Tkinter programs with: tk.mainloop(), or nothing would show up! See example: from Tkinter import * import random import time tk = Tk() tk.title = "Game" tk.resizable(0,0) tk.wm_attributes("-topmost", 1) canvas =…
midkin
  • 1,503
  • 2
  • 18
  • 23
100
votes
14 answers

Tkinter module not found on Ubuntu

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Tkinter >>> on the other hand... Python 3.2 (r32:88445, Mar 25 2011, 19:28:28) [GCC 4.5.2]…
Jim Syyap
  • 1,625
  • 7
  • 20
  • 23
98
votes
8 answers

How can I schedule updates (f/e, to update a clock) in tkinter?

I'm writing 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); when I use time.sleep() in a loop the whole…
Diego Castro
  • 3,458
  • 4
  • 35
  • 42
97
votes
3 answers

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…
Simon Hibbs
  • 5,941
  • 5
  • 26
  • 32
92
votes
8 answers

Tkinter scrollbar for frame

My objective is to add a vertical scroll bar to a frame which has several labels in it. The scroll bar should automatically enabled as soon as the labels inside the frame exceed the height of the frame. After searching through, I found this useful…
Chris Aung
  • 9,152
  • 33
  • 82
  • 127
91
votes
13 answers

Is there a way to make the Tkinter text widget read only?

It doesn't look like it has that attribute, but it'd be really useful to me.
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
88
votes
15 answers

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.
psicopoo
  • 1,576
  • 1
  • 12
  • 20
88
votes
8 answers

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…
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147