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
87
votes
9 answers

How do I get an event callback when a Tkinter Entry widget is modified?

Exactly as the question says. Text widgets have the <> event, but Entry widgets don't appear to.
bfops
  • 5,348
  • 5
  • 36
  • 48
86
votes
5 answers

Why does Tkinter image not show up if created in a function?

This code works: import tkinter root = tkinter.Tk() canvas = tkinter.Canvas(root) canvas.grid(row = 0, column = 0) photo = tkinter.PhotoImage(file = './test.gif') canvas.create_image(0, 0, image=photo) root.mainloop() It shows me the image. Now,…
thomas.winckell
  • 1,217
  • 1
  • 10
  • 16
86
votes
4 answers

Why do I get "AttributeError: NoneType object has no attribute" using Tkinter? Where did the None value come from?

I've created this simple GUI: from tkinter import * root = Tk() def grabText(event): print(entryBox.get()) entryBox = Entry(root, width=60).grid(row=2, column=1, sticky=W) grabBtn = Button(root, text="Grab") grabBtn.grid(row=8,…
Arnkrishn
  • 29,828
  • 40
  • 114
  • 128
83
votes
4 answers

List of All Tkinter Events

In Python tkinter module, , and are used to identify mouse button clicks for left, middle and right buttons respectively. Likewise, is used for the return key press. Where can I find a list of all…
mcu
  • 3,302
  • 8
  • 38
  • 64
81
votes
12 answers

Using Tkinter in python to edit the title bar

I am trying to add a custom title to a window but I am having troubles with it. I know my code isn't right but when I run it, it creates 2 windows instead, one with just the title tk and another bigger window with "Simple Prog". How do I make it so…
Dan
  • 8,263
  • 16
  • 51
  • 53
79
votes
6 answers

What is the difference between the widgets of tkinter and tkinter.ttk in Python?

The main tkinter module and its submodule ttk in Python 3 appear to contain identical widgets (i.e. Buttons, CheckButtons, etc.). So, when creating a button, one has the freedom to either use a tkinter.Button widget or a tkinter.ttk.Button. Do you…
multigoodverse
  • 7,638
  • 19
  • 64
  • 106
78
votes
12 answers

How to make a Tkinter window jump to the front?

How do I get a Tkinter application to jump to the front? Currently, the window appears behind all my other windows and doesn't get focus. Is there some method I should be calling?
nathan
  • 4,612
  • 6
  • 32
  • 33
76
votes
2 answers

Equivalent function for xticks for an AxesSubplot object

So I am trying to use Axes objects to control my matlibplot figure. I am not using plt (aka import matlibplot.pyplot as plt) because I am embedding the figure in my tkinter GUI per this. However, I am also using subplots in the figure, so something…
tylerthemiler
  • 5,496
  • 6
  • 32
  • 40
74
votes
5 answers

Tkinter: How to use threads to preventing main event loop from "freezing"

I have a small GUI test with a "Start" button and a Progress bar. The desired behavior is: Click Start Progressbar oscillates for 5 seconds Progressbar stops The observed behavior is the "Start" button freezes for 5 seconds, then a Progressbar is…
Dirty Penguin
  • 4,212
  • 9
  • 45
  • 69
73
votes
3 answers

How to update the image of a Tkinter Label widget?

I would like to be able to swap out an image on a Tkinter label, but I'm not sure how to do it, except for replacing the widget itself. Currently, I can display an image like so: import Tkinter as tk import ImageTk root = tk.Tk() img =…
skeggse
  • 6,103
  • 11
  • 57
  • 81
72
votes
10 answers

How do I display tooltips in Tkinter?

Tooltips are those little bits of text that popup when the mouse hovers over a widget for a certain duration of time. How can I add a tooltip message to my tkinter Python application?
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
72
votes
9 answers

How to clear/delete the contents of a Tkinter Text widget?

I am writing a Python program in TKinter on Ubuntu to import and print the name of files from particular folder in Text widget. It is just adding filenames to the previous filnames in the Text widget, but I want to clear it first, then add a fresh…
Fahadkalis
  • 2,971
  • 8
  • 23
  • 40
72
votes
7 answers

How to specify where a Tkinter window opens?

How can I tell a Tkinter window where to open, based on screen dimensions? I would like it to open in the middle.
xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60
70
votes
3 answers

Difference between "fill" and "expand" options for tkinter pack method

What's the difference between the "fill" and "expand" options for Tkinter's pack method? I have actually looked up about it everywhere, and I am unable to find the satisfactory answer. I found the following: fill option: it determines whether to…
Python_user
  • 1,378
  • 3
  • 12
  • 25
68
votes
8 answers

Display fullscreen mode on Tkinter

How can I make a frame in Tkinter display in fullscreen mode? I saw this code, and it's very useful…: >>> import Tkinter >>> root = Tkinter.Tk() >>> root.overrideredirect(True) >>> root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(),…
DRdr
  • 1,171
  • 3
  • 12
  • 12