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
41
votes
6 answers

Tkinter KeyPress and KeyRelease events

I understood that the Tk keypress and keyrelease events were supposed only to fire when the key was actually pressed or released? However with the following simple code, if I hold down the "a" key I get a continual sequence of alternating…
lost
  • 2,210
  • 2
  • 20
  • 34
40
votes
2 answers

PySide / PyQt detect if user trying to close window

is there a way to detect if user trying to close window? For example, in Tkinter we can do something like this: def exit_dialog(): #do stuff pass root = Tk() root.protocol("WM_DELETE_WINDOW", exit_dialog) root.mainloop() Thanks.
SaulTigh
  • 913
  • 2
  • 14
  • 27
40
votes
2 answers

tkinter gui layout using frames and grid

My gui layout looks almost nothing like what I expect so I assume there are some basics that I don't understand. I assumed that frames contain their own 'grid space' (row, column) but the behavior I see doesn't bear that out, and I'm at a loss…
shawn
  • 549
  • 1
  • 5
  • 11
40
votes
7 answers

How to pass an argument to event handler in tkinter?

widget.bind('',callback) # binding def callback(self,event) #do something I need to pass an argument to callback() . The argument is a dictionary object.
sag
  • 445
  • 1
  • 5
  • 6
40
votes
4 answers

How to clear an entire Treeview with Tkinter

My program uses a ttk.Treeview as a table and fills it with many numbers. I want to clear the ttk.Treeview when I press a button in the window. Is there a simple way to clear the ttk.Treeview? Thanks.
katze
  • 1,273
  • 3
  • 16
  • 24
39
votes
11 answers

How to draw polygons with Python?

I have input values of x, y coordinates in the following format: [[1,1], [2,1], [2,2], [1,2], [0.5,1.5]] I want to draw polygons, but I don't know how to draw them! Thanks
W.Fan
  • 407
  • 1
  • 4
  • 3
39
votes
4 answers

Mouse Position Python Tkinter

Is there a way to get the position of the mouse and set it as a var?
Kyle Pfromer
  • 1,485
  • 1
  • 15
  • 26
39
votes
8 answers

Display message when hovering over something with mouse cursor in Python

I have a GUI made with TKinter in Python. I would like to be able to display a message when my mouse cursor goes, for example, on top of a label or button. The purpose of this is to explain to the user what the button/label does or represents. Is…
maupertius
  • 1,518
  • 4
  • 17
  • 30
39
votes
2 answers

How do I change the background of a Frame in Tkinter?

I have been creating an Email program using Tkinter, in Python 3.3. On various sites I have been seeing that the Frame widget can get a different background using Frame.config(background="color"). However, when I use this in my Frames it gives the…
IPDGino
  • 706
  • 1
  • 8
  • 17
38
votes
3 answers

Inheriting from Frame or not in a Tkinter application

I've seen two basic ways of setting up a tkinter program. Is there any reason to prefer one to the other? from Tkinter import * class Application(): def __init__(self, root, title): self.root = root self.root.title(title) …
foosion
  • 7,619
  • 25
  • 65
  • 102
38
votes
4 answers

ImportError: libtk8.6.so: cannot open shared object file: No such file or directory

I am trying to add a GUI input box and I found out that the way you do that is by using a module called tkinter. While I was trying to install it on my arch linux machine through the ActivePython package I got the following error: Traceback (most…
KnownAsDon
  • 1,365
  • 1
  • 8
  • 14
38
votes
3 answers

TypeError: generatecode() takes 0 positional arguments but 1 was given

I have the code below: from tkinter import * class Window(Frame): def __init__(self, master = None): Frame.__init__(self, master) self.master = master self.init_window() def init_window(self): …
Jason Martin
  • 578
  • 1
  • 4
  • 13
38
votes
2 answers

Border for tkinter Label

I'm building a calendar that would look much nicer if it had some borders for its Labels! I have seen you can do this for other widgets such as Button, Entry and Text. Minimal code: from tkinter import * root = Tk() L1 = Label(root,…
Pax Vobiscum
  • 2,551
  • 2
  • 21
  • 32
37
votes
9 answers

How to put a tkinter window on top of the others?

I'm using Python 2 with Tkinter and PyObjC, and then I'm using py2app. The program is working fine, but the window starts as hidden whenever I open the program, so it doesn't appear until I click on the icon on the dock to bring it up. Is there…
Dennis
  • 3,506
  • 5
  • 34
  • 42
37
votes
1 answer

AttributeError: 'set' object has no attribute 'items'

I am very new to python and have been trying to teach myself as I go (not the best method this deep into python but for time's sake I need too). The modules I've imported are Tkinter and csv. Let me know if you have any questions, For the sake of…
MTMA
  • 373
  • 1
  • 3
  • 5