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

How to create a hyperlink with a Label in Tkinter?

How do you create a hyperlink using a Label in Tkinter? A quick search did not reveal how to do this. Instead there were only solutions to create a hyperlink in a Text widget.
James Burke
  • 2,229
  • 1
  • 26
  • 34
28
votes
5 answers

Making python/tkinter label widget update?

I'm working on getting a python/tkinter label widget to update its contents. Per an earlier thread today, I followed instructions on how to put together the widgets. At runtime, however, the label widget does NOT change contents, but simply retains…
Stephen Gross
  • 5,274
  • 12
  • 41
  • 59
28
votes
2 answers

Setting focus to specific TKinter entry widget

I'd like to set the focus of my program to a specific entry widget so that I can start entering data straight-away - how can I do this? My current code from Tkinter import * root = Tk() frame=Frame(root,width=100,heigh=100,bd=11) frame.pack() label…
Leo
  • 649
  • 3
  • 9
  • 20
28
votes
2 answers

Showing and Hiding widgets

How do you show and hide widgets in Tkinter? I want to have an entry box, but not have it shown at all times. Can someone show me the functions to show and hide entry widgets and other widgets in tkinter? I want to be able to do this without…
udpatil
  • 529
  • 2
  • 6
  • 14
27
votes
4 answers

Redirect command line results to a tkinter GUI

I have created a program that prints results on command line. (It is server and it prints log on command line.) Now, I want to see the same result to GUI . How can I redirect command line results to GUI? Please, suggest a trick to easily transform…
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
27
votes
5 answers

How can I make silent exceptions louder in tkinter?

If I run the following code from a terminal, I get a helpful error message in the terminal: import Tkinter as tk master = tk.Tk() def callback(): raise UserWarning("Exception!") b = tk.Button(master, text="This will raise an exception",…
Andrew
  • 2,842
  • 5
  • 31
  • 49
27
votes
3 answers

Python matplotlib - setting x-axis scale

I have this graph displaying the following: plt.plot(valueX, scoreList) plt.xlabel("Score number") # Text for X-Axis plt.ylabel("Score") # Text for Y-Axis plt.title("Scores for the topic "+progressDisplay.topicName) plt.show() valueX = [1, 2, 3, 4]…
YusufChowdhury
  • 309
  • 1
  • 3
  • 10
27
votes
11 answers

List available font families in `tkinter`

In many tkinter examples available out there, you may see things like: canvas.create_text(x, y, font=('Helvetica', 12), text='foo') However, this may not work when run in your computer (the result would completely ignore the font parameter).…
Peque
  • 13,638
  • 11
  • 69
  • 105
27
votes
4 answers

Problem running python/matplotlib in background after ending ssh session

I have to VPN and then ssh from home to my work server and want to run a python script in the background, then log out of the ssh session. My script makes several histogram plots using matplotlib, and as long as I keep the connection open everything…
Jamie
  • 579
  • 1
  • 9
  • 15
27
votes
6 answers

Tkinter adding line number to text widget

Trying to learn tkinter and python. I want to display line number for the Text widget in an adjacent frame from Tkinter import * root = Tk() txt = Text(root) txt.pack(expand=YES, fill=BOTH) frame= Frame(root, width=25) # frame.pack(expand=NO,…
bhaskarc
  • 9,269
  • 10
  • 65
  • 86
27
votes
5 answers

In python's tkinter, how can I make a Label such that you can select the text with the mouse?

In python's tkinter interface, is there a configuration option that will change a Label such that you can select the text in the Label and then copy it to the clipboard? EDIT: How would you modify this "hello world" app to provide such…
Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
27
votes
6 answers

Why is Tkinter Entry's get function returning nothing?

I'm trying to use an Entry field to get manual input, and then work with that data. All sources I've found claim I should use the get() function, but I haven't found a simple working mini example yet, and I can't get it to work. I hope someone can…
CodingCat
  • 4,999
  • 10
  • 37
  • 59
27
votes
7 answers

Correct way to implement a custom popup tkinter dialog box

I just started learning how to create a custom pop up dialog box; and as it turns out, the tkinter messagebox is really easy to use, but it also does not do too much. Here is my attempt to create a dialog box that will take input and then store that…
George
  • 4,514
  • 17
  • 54
  • 81
26
votes
6 answers

Way to play video files in Tkinter?

Is there a way to play video files like AVI, MP4, etc.? I tried using PyMedia, but apparently it only works with Pygame. What is the solution to my problem?
P'sao
  • 2,946
  • 11
  • 39
  • 48
26
votes
3 answers

Deleting and changing a tkinter event binding

How do i stop an event from being processed or switch what function is called for it? Revised Code: from Tkinter import * class GUI: def __init__(self,root): Window = Frame(root) self.DrawArea = Canvas(Window) …
Symon
  • 2,170
  • 4
  • 26
  • 34