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

How to set a tkinter window to a constant size

I'm programming a little game with tkinter and briefly, I'm stuck. I have a kind od starting menu, in which are two buttons and one label. If I just create the frame everything is fine, it has the size 500x500 pixels I want the background not to…
Mr. Squiddy
  • 321
  • 1
  • 3
  • 7
31
votes
6 answers

How to determine what version of python3 tkinter is installed on my linux machine?

Hi have been scavenging the web for answers on how to do this but there was no direct answer. Does anyone know how I can find the version number of tkinter?
Tom Boy
  • 599
  • 2
  • 8
  • 14
31
votes
4 answers

Why does the calculated width and height in pixel of a string in Tkinter differ between platforms?

I have a Python script which needs to calculate the exact size of arbitrary strings displayed in arbitrary fonts in order to generate simple diagrams. I can easily do it with Tkinter. import Tkinter as tk import tkFont root = tk.Tk() canvas =…
Aristide
  • 3,606
  • 2
  • 30
  • 50
31
votes
3 answers

Drag and drop explorer files to tkinter entry widget?

I'm fairly new to Python. I'm trying to input a file name (complete with full path) to a TKinter entry widget. Since the path to the file name can be very long I would like to be able to drag and drop the file directly from Windows Explorer. In…
George Joseph
  • 311
  • 1
  • 3
  • 4
31
votes
4 answers

How do I set the initial window size as the minimum window size in tkinter?

My understanding is that after initializing all frames and widgets in the __init__ method, the tkinter window resizes to fit all these components. I would like to set the window's initialized size to be its minimum size. I want to be able to…
Jace Browning
  • 11,699
  • 10
  • 66
  • 90
30
votes
6 answers

How to close a Tkinter window by pressing a Button?

Write a GUI application with a button labeled "Good-bye". When the Button is clicked, the window closes. This is my code so far, but it is not working. Can anyone help me out with my code? from Tkinter import * window = Tk() def close_window…
Matt Hawk
  • 317
  • 1
  • 4
  • 4
30
votes
7 answers

How can I convert canvas content to an image?

from Tkinter import * root = Tk() cv = Canvas(root) cv.create_rectangle(10,10,50,50) cv.pack() root.mainloop() I want to convert canvas content to a bitmap or other image, and then do other operations, such as rotating or scaling the image, or…
liupeixin
  • 718
  • 1
  • 9
  • 15
30
votes
3 answers

How to create a multiline entry with tkinter?

Entry widgets seem only to deal with single line text. I need a multiline entry field to type in email messages. Anyone has any idea how to do that?
xiaolong
  • 3,396
  • 4
  • 31
  • 46
30
votes
5 answers

Token error: EOF in multi-line statement

The following code gives me this error "Token Error: EOF in multi-line statement". What is this error? How can I fix it? import easygui import time namegui = easygui.enterbox(msg='Enter your name:', title='Name query', default='Gian') situationgui =…
gian848396
  • 459
  • 1
  • 8
  • 24
30
votes
2 answers

How to get the Tkinter Label text?

Im making a list of addresses that the user will select from, and the address text will be returned. I need to use Tkinter.Label because the Tkinter.Listbox will not allow for newlines. The kicker is there is no .get()-like method in the Label…
lmno
  • 1,004
  • 1
  • 15
  • 27
30
votes
4 answers

Where can I find API documentation for tkinter?

I am looking for a complete online reference for Python's tkinter module. I've tried to find one by myself, but for some reason I can't. To clarify, I am not looking for a tutorial. Instead, I would like to have a reference for all the available…
kynikos
  • 1,152
  • 4
  • 14
  • 25
30
votes
8 answers

Use asyncio and Tkinter (or another GUI lib) together without freezing the GUI

I want to use asyncio in combination with a tkinter GUI. I am new to asyncio and my understanding of it is not very detailed. The example here starts 10 task when clicking on the first button. The task are just simulating work with a sleep() for…
buhtz
  • 10,774
  • 18
  • 76
  • 149
30
votes
6 answers

Tkinter: is there a way to check checkboxes by default?

I have this piece of code that will create a simple checkbox : from Tkinter import * CheckVar = IntVar() self.checkbutton = Checkbutton(self.root, text = "Test", variable = CheckVar) However this checkbox in unchecked by default and I'm searching…
Marvin Lerousseau
  • 363
  • 2
  • 4
  • 9
30
votes
5 answers

Removing minimize/maximize buttons in Tkinter

I have a python program which opens a new windows to display some 'about' information. This window has its own close button, and I have made it non-resizeable. However, the buttons to maximize and minimize it are still there, and I want them gone.…
Vidar
  • 4,141
  • 5
  • 24
  • 30
30
votes
1 answer

No display name and no $DISPLAY environment variable using tkinter through ssh

I'm trying to run a very simple code that outputs a .png file in a cluster. Here's the code: import matplotlib.pyplot as plt import numpy as np x = np.random.randn(60) y = np.random.randn(60) plt.scatter(x, y, s=20) out_png =…
Gabriel
  • 40,504
  • 73
  • 230
  • 404