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
412
votes
28 answers

ImportError: No module named 'Tkinter'

For some reason, I can't use the Tkinter (or tkinter, on Python 3) module. After running the following command in the python shell: import Tkinter or this, in Python 3: import tkinter I got this error ModuleNotFoundError: No module named…
RasmusGP
  • 4,696
  • 5
  • 21
  • 28
344
votes
25 answers

Install tkinter for Python

I am trying to import Tkinter. However, I get an error stating that Tkinter has not been installed: ImportError: No module named _tkinter, please install the python-tk package I could probably install it using synaptic manager (can I?), however, I…
Karan
  • 14,824
  • 24
  • 91
  • 157
309
votes
14 answers

Create a directly-executable cross-platform GUI app using Python

Python works on multiple platforms and can be used for desktop and web applications, thus I conclude that there is some way to compile it into an executable for Mac, Windows and Linux. The problem being I have no idea where to start or how to write…
Teifion
  • 108,121
  • 75
  • 161
  • 195
268
votes
14 answers

How to pass arguments to a Button command in Tkinter?

Suppose I have the following Button made with Tkinter in Python: import Tkinter as Tk win = Tk.Toplevel() frame = Tk.Frame(master=win).grid(row=1, column=1) button = Tk.Button(master=frame, text='press', command=action) The method action is called…
Jack
  • 5,264
  • 7
  • 34
  • 43
241
votes
11 answers

_tkinter.TclError: no display name and no $DISPLAY environment variable

I am running a simple python script in the server: 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 = 'path/to/store/out_file.png' plt.savefig(out_png, dpi=150) I…
Po-Yu Kao
  • 2,597
  • 2
  • 12
  • 14
226
votes
19 answers

matplotlib error - no module named tkinter

I tried to use the matplotlib package via Pycharm IDE on windows 10. when I run this code: from matplotlib import pyplot I get the following error: ImportError: No module named 'tkinter' I know that in python 2.x it was called Tkinter, but that is…
noamgot
  • 3,962
  • 4
  • 24
  • 44
215
votes
9 answers

What is the best way to structure a Tkinter application?

The following is the overall structure of my typical Python Tkinter program. def funA(): def funA1(): def funA12(): # stuff def funA2(): # stuff def funB(): def funB1(): # stuff def funB2(): …
Chris Aung
  • 9,152
  • 33
  • 82
  • 127
212
votes
8 answers

How to update a plot in matplotlib

I'm having issues with redrawing the figure here. I allow the user to specify the units in the time scale (x-axis) and then I recalculate and call this function plots(). I want the plot to simply update, not append another plot to the figure. def…
thenickname
  • 6,684
  • 14
  • 41
  • 42
200
votes
11 answers

How do I handle the window close event in Tkinter?

How do I handle the window close event (user clicking the 'X' button) in a Python Tkinter program?
Matt Gregory
  • 8,074
  • 8
  • 33
  • 40
169
votes
18 answers

How can I create a simple message box in Python?

I'm looking for the same effect as alert() in JavaScript. I wrote a simple web-based interpreter this afternoon using Twisted Web. You basically submit a block of Python code through a form, and the client comes and grabs it and executes it. I want…
Carson Myers
  • 37,678
  • 39
  • 126
  • 176
158
votes
9 answers

How to get the input from the Tkinter Text Widget?

How to get Tkinter input from the Text widget? EDIT I asked this question to help others with the same problem - that is the reason why there is no example code. This issue had been troubling me for hours and I used this question to teach others.…
xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60
157
votes
5 answers

How do you run your own code alongside Tkinter's event loop?

My little brother is just getting into programming, and for his Science Fair project, he's doing a simulation of a flock of birds in the sky. He's gotten most of his code written, and it works nicely, but the birds need to move every…
Allan S
  • 1,681
  • 2
  • 11
  • 7
154
votes
22 answers

Tkinter: "Python may not be configured for Tk"

Today I wanted to start working with Tkinter, but I have some problems. Python 3.2 (r32:88445, Mar 28 2011, 04:14:07) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from tkinter import * Traceback…
Maciej Ziarko
  • 11,494
  • 13
  • 48
  • 69
145
votes
4 answers

Dynamically updating plot in matplotlib

I am making an application in Python which collects data from a serial port and plots a graph of the collected data against arrival time. The time of arrival for the data is uncertain. I want the plot to be updated when data is received. I searched…
Shadman Anwer
  • 1,706
  • 3
  • 15
  • 17
138
votes
14 answers

How to pip or easy_install tkinter on Windows

IDLE is throwing errors that and says tkinter can't be imported. Is there a simple way to install tkinter via pip or easy_install? There seem to be a lot of package names flying around for this... This and other assorted variations with tkinter-pypy…
Dirk Calloway
  • 2,569
  • 4
  • 23
  • 34
1
2 3
99 100