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

How to create a self resizing grid of buttons in tkinter?

I am trying to create a grid of buttons(in order to achieve the clickable cell effect) with Tkinter. My main problem is that I cannot make the grid and the buttons autoresize and fit the parent window. For example, when I have a high number of…
Kiril
  • 2,091
  • 7
  • 33
  • 43
62
votes
10 answers

Windows- Pyinstaller Error "failed to execute script " When App Clicked

I am having a tough time overcoming this error, I have searched everywhere for that error message and nothing seems relevant to my situation: "failed to execute script new-app" new-app is my python GUI program. When I run pyinstaller using this…
aBiologist
  • 2,007
  • 2
  • 14
  • 21
62
votes
5 answers

How to create a password entry field using Tkinter

I am trying to code a login window using Tkinter but I'm not able to hide the password text in asterisk format. This means the password entry is plain text, which has to be avoided. Any idea how to do it?
Hick
  • 35,524
  • 46
  • 151
  • 243
61
votes
3 answers

Getting a callback when a Tkinter Listbox selection is changed?

There are a number of ways of getting callbacks when Text or Entry widgets are changed in Tkinter, but I haven't found one for Listbox's (it doesn't help that much of the event documentation I can find is old or incomplete). Is there some way of…
bfops
  • 5,348
  • 5
  • 36
  • 48
60
votes
3 answers

Getting Checkbutton state

How do I get the 'state' of a Tkinter Checkbutton? By 'state' I mean get whether or not it has a check mark in it or not.
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
59
votes
8 answers

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…
Malcolm
  • 5,125
  • 10
  • 52
  • 75
58
votes
3 answers

How to clear Tkinter Canvas?

When I draw a shape using: canvas.create_rectangle(10, 10, 50, 50, color="green") Does Tkinter keep track of the fact that it was created? In a simple game I'm making, my code has one Frame create a bunch of rectangles, and then draw a big black…
Taylor Hill
  • 1,053
  • 1
  • 14
  • 24
58
votes
3 answers

Python Tkinter clearing a frame

I am trying to clear out a frame in the tkinter so that the new contents can be written (refresh information) but i could not manage to do it. I am aware of these frame.destroy() frame.pack_forget() frame.grid_forget() but frame.destroy() will…
Chris Aung
  • 9,152
  • 33
  • 82
  • 127
57
votes
1 answer

What does 'weight' do in tkinter?

I've been searching different websites trying to find out what weight does in tkinter. I got this from TkDocs: Every column and row has a "weight" grid option associated with it, which tells it how much it should grow if there is extra room in the…
Fred
  • 750
  • 1
  • 7
  • 14
57
votes
8 answers

In Tkinter is there any way to make a widget invisible?

Something like this, would make the widget appear normally: Label(self, text = 'hello', visible ='yes') While something like this, would make the widget not appear at all: Label(self, text = 'hello', visible ='no')
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
56
votes
4 answers

Fresh tutorial on tkinter and ttk for Python 3

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.
Oleh Prypin
  • 33,184
  • 10
  • 89
  • 99
55
votes
5 answers

Colour chart for Tkinter and Tix

I wanted to visualise some basic colours, so I could pick the appropriate ones for my colour scheme. I couldn't find a colour chart anywhere so modified a sample to display it. Is this a good way to do it? import Tix as tk # import tkinter.tix as tk…
user600295
  • 985
  • 3
  • 10
  • 14
55
votes
2 answers

How can I create a dropdown menu from a List in Tkinter?

I am creating a GUI that builds information about a person. I want the user to select their birth month using a drop down bar, with the months configured earlier as a list format. from tkinter import * birth_month = [ 'Jan', 'Feb', …
user7437114
55
votes
13 answers

How to clear the Entry widget after a button is pressed in Tkinter?

I'm trying to clear the Entry widget after the user presses a button using Tkinter. I tried using ent.delete(0, END), but I got an error saying that strings don't have the attribute delete. Here is my code, where I'm getting error on real.delete(0,…
Dan
  • 8,263
  • 16
  • 51
  • 53
55
votes
6 answers

How to set the text/value/content of an `Entry` widget using a button in tkinter

I am trying to set the text of an Entry widget using a button in a GUI using the tkinter module. This GUI is to help me classify thousands of words into five categories. Each of the categories has a button. I was hoping that using a button would…
unlockme
  • 3,897
  • 3
  • 28
  • 42