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

Installing tkinter on ubuntu 14.04

I want to run python code on Ubuntu 14.04, but when I execute it, it gives me the following error message Traceback (most recent call last): File "main.py", line 2, in from tkinter import * ImportError: No module named tkinter
Mayank Jain
  • 1,057
  • 2
  • 9
  • 6
45
votes
4 answers

What is the difference between root.destroy() and root.quit()?

In Python using tkinter, what is the difference between root.destroy() and root.quit() when closing the root window? Is one prefered over the other? Does one release resources that the other doesn't?
Gary Willoughby
  • 50,926
  • 41
  • 133
  • 199
44
votes
4 answers

How to create beautiful UI's with Python

I wonder if it's possible to create good looking desktop UI's with python? Could I use JS frameworks like Electron with python? Or are there any python libraries which provide modern looking and easy to use widgets? This is what I have in mind for…
Eren Arıcı
  • 749
  • 1
  • 6
  • 12
44
votes
5 answers

TkMessageBox - No Module

import TkMessageBox When I import TkMessageBox it displays the messsge 'ImportError: No module named 'TkMessageBox'. As far as I know im using python 3.3.2 and Tk 8.5. Am I using the wrong version of python or importing it wrong ? Any answers would…
Tom Lowbridge
  • 879
  • 3
  • 9
  • 17
44
votes
1 answer

How do I change the text size in a Label widget? (tkinter)

In python 3.4 using Tkinter, how do I change the text size in a label widget? So far I have tried label_one = Label(root, text = 'Hello', size = '50') and label_one.config(fontsize='50') But I am not sure where to start and I can't find anything…
james hughes
  • 597
  • 2
  • 5
  • 11
44
votes
5 answers

Tkinter example code for multiple windows, why won't buttons load correctly?

I am writing a program which should: Open a window with the press of a button. Close the newly opened window with the press of another button. I'm using classes so I can insert the code into a larger program later. However, I can't get my buttons…
ADB
  • 1,210
  • 4
  • 15
  • 23
44
votes
3 answers

Closing pyplot windows

Final Edit: What I found on the subject of closing pyplot windows is that it really probably shouldn't be done using pyplot. SRK gives a great example on how to handle plots that will be updated in his answer below. Also I have stumbled across…
deadstump
  • 955
  • 2
  • 11
  • 23
43
votes
8 answers

Nice IDE with GUI designer for wxPython or Tkinter

I have a little experience developing small command-line applications with Python. I want to move on to developing GUIs with Python. From the available GUI toolkits for Python, the ones I feel the most inclined to are wxPython and Tkinter; but I…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
43
votes
1 answer

Tkinter assign button command in a for loop with lambda

I'm trying to create a few Buttons (with a for loop) like so: def a(self, name): print(name) users = {"Test": "127.0.0.0", "Test2": "128.0.0.0"} row = 1 for name in users: user_button = Tkinter.Button(self.root, text=name, …
Shay
  • 1,375
  • 5
  • 16
  • 26
43
votes
10 answers

How to add an image in Tkinter?

How do I add an image in Tkinter? This gave me a syntax error: root = tk.Tk() img = ImageTk.PhotoImage(Image.open(path)) panel = tk.Label(root, image = img) panel.pack(side = "bottom", fill = "both", expand = "yes") root.mainloop()
Damien
  • 563
  • 2
  • 6
  • 10
42
votes
3 answers

How can I get the screen size in Tkinter?

I would like to know if it is possible to calculate the screen size using Tkinter. I wanted this so that can make the program open up in the center of the screen...
DonJuma
  • 2,028
  • 13
  • 42
  • 70
42
votes
3 answers

tkinter: how to use after method

Hey I am new to python and am using tkinter for my gui. I am having trouble using the "after" method. The goal is to make a random letter appear every 5 seconds. Here is my code: import random import time from tkinter import * root = Tk() w =…
user2456977
  • 3,830
  • 14
  • 48
  • 87
42
votes
3 answers

Cannot use geometry manager pack inside

So I'm making an rss reader using the tkinter library, and in one of my methods I create a text widget. It displays fine until I try to add scrollbars to it. Here is my code before the scrollbars: def create_text(self, root): …
user3623888
  • 535
  • 1
  • 4
  • 5
42
votes
5 answers

Tkinter messagebox without window?

I want to show an info window in my python script running on ubuntu. I'm using the following code: import tkMessageBox tkMessageBox.showinfo("Say Hello", "Hello World") This works, but there's an empty window displayed, with the message box on top.…
user1491250
  • 1,831
  • 4
  • 18
  • 21
42
votes
3 answers

How to change Tkinter Button state from disabled to normal?

I need to change the state of a Button from DISABLED to NORMAL when some event occurs. The button is currently created in the DISABLED state using the following code: self.x = Button(self.dialog, text="Download", state=DISABLED, …
scandalous
  • 912
  • 5
  • 14
  • 25