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
30
votes
2 answers

Simpler way to draw a circle with tkinter?

Drawing a circle on a tkinter Canvas is usually done by the create_oval method. However, supplying the bounding box is often a confusing way to think about drawing a circle. It's not particularly difficult to come up with a shortcut for it, but I…
mgold
  • 6,189
  • 4
  • 39
  • 41
30
votes
4 answers

open multiple filenames in tkinter and add the filesnames to a list

what I want to do is to select multiple files using the tkinter filedialog and then add those items to a list. After that I want to use the list to process each file one by one. #replace.py import string def main(): #import tkFileDialog …
faraz
  • 2,603
  • 12
  • 39
  • 61
30
votes
1 answer

How to keep selections highlighted in a tkinter Listbox?

I have 2 separated List-boxes set on single selection mode. When I select an item from listboxA, it gets highlighted, but when I select an item from listboxB, it gets highlighted, and the item from listboxA remains active, but isn't highlighted. How…
Mission Impossible
  • 377
  • 3
  • 6
  • 10
29
votes
2 answers

Attempting to resolve blurred tkinter text + scaling on Windows 10 high DPI displays, but concerned my approach is not Pythonic or is unsafe

After hours of tweaking I have settled on this code which allows me to get round the familiar problem of blurry / fuzzy text in Windows 10 on high DPI displays when using Tkinter interfaces in Python 3. I didn't want to have to set the compatibility…
dingles
  • 1,548
  • 1
  • 14
  • 11
29
votes
3 answers

Tkinter button command activates upon running program?

I'm trying to make a build retrieval form, and seem to have issues with the buttons... I'm a novice at Python/tkinter GUI programming (and GUI programming in general) and borrowed the skeleton of a Hello World app, and sorta built off that. In the…
Doktor J
  • 1,058
  • 1
  • 14
  • 33
29
votes
4 answers

Overriding Tkinter "X" button control (the button that close the window)

When the user presses a close Button that I created, some tasks are performed before exiting. However, if the user clicks on the [X] button in the top-right of the window to close the window, I cannot perform these tasks. How can I override what…
erkangur
  • 2,048
  • 5
  • 21
  • 31
29
votes
4 answers

Tkinter - Can't bind arrow key events

I am trying to bind the left and right arrow keys to an event in Tkinter, but when I run the program it appears the events are not triggering. Here is the code: from Tkinter import * main = Tk() def leftKey(event): print "Left key…
aftrumpet
  • 1,189
  • 2
  • 16
  • 28
29
votes
3 answers

tkinter ttk separator won't display

Consider this simple code: from Tkinter import * import ttk root= Tk() ttk.Label(root, text='Heading Here').grid(row=1, column=1) ttk.Separator(root,orient=HORIZONTAL).grid(row=2, columnspan=5) root.mainloop() When I run this code, the separator is…
bhaskarc
  • 9,269
  • 10
  • 65
  • 86
29
votes
5 answers

tkinter listbox get(ACTIVE) method

I was trying to make the currently selected Listbox item to be printed out. For example, when I select item "one", it should print out "one" and when I select item "two", it should print out "two" etc. The following is what I have tried. from…
Chris Aung
  • 9,152
  • 33
  • 82
  • 127
29
votes
4 answers

How do I use Tkinter to create line-wrapped text that fills the width of the window?

The Label widget doesn't line-wrap. The Message widget will line-wrap text, but forces it to be roughly square. Here's an example: from Tkinter import * root = Tk() root.title("hello") Message(root, text=48*'xxxxx ').grid(row=0, column=0,…
samwyse
  • 2,760
  • 1
  • 27
  • 38
28
votes
7 answers

Removing the TK icon on a Tkinter window

How to remove tkinter icon from title bar in it's window
Evan Fosmark
  • 98,895
  • 36
  • 105
  • 117
28
votes
7 answers

No matching distribution found for tkinter

I am stuck with this issue since last two days and I have tried every possible solution on the stack and github. It will be really great if someone can recommend. I am working with python 2.7 in a virtual environment on CentOS Linux release…
AnkP
  • 631
  • 2
  • 9
  • 18
28
votes
7 answers

Matplotlib Crashing tkinter Application

I am building an application that embeds a matplotlib figure into the GUI. The problem is that my app is crashing as soon as I add anything from matplotlib into my code (except for the imports, those work as usual). The problem occurs in my class…
Camon
  • 1,003
  • 1
  • 10
  • 22
28
votes
3 answers

Tkinter: How to get frame in canvas window to expand to the size of the canvas?

So I've been using the canvas widget in tkinter to create a frame full of labels which has a scrollbar. All is working good except that the frame only expands to the size of the labels placed in it - I want the frame to expand to the size of the…
Jay Jen
  • 705
  • 1
  • 7
  • 11
28
votes
1 answer

What's the difference between "update" and "update_idletasks"?

From the effbot.org documentation, we have the following about the update function: Processes all pending events, calls event callbacks, completes any pending geometry management, redraws widgets as necessary, and calls all pending idle tasks. This…
nbro
  • 15,395
  • 32
  • 113
  • 196