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
34
votes
8 answers

How to replace the icon in a Tkinter app?

I am using Python 3.5.0 on Windows 10 and want to replace this:
It's Willem
  • 656
  • 3
  • 8
  • 15
34
votes
1 answer

How to bind events to Canvas items?

If I'm using a canvas to display data and I want the user to be able to click on various items on the canvas in order to get more information or interact with it in some way, what's the best way of going about this? Searching online I can find…
Ian
  • 363
  • 1
  • 4
  • 6
34
votes
1 answer

Disable the underlying window when a popup is created in Python TKinter

I have a master Frame (call it a), and a popup Toplevel (call it b). How do I make sure the user cannot click on anything in a while b is "alive"?
Daniel Kats
  • 5,141
  • 15
  • 65
  • 102
33
votes
2 answers

tkinter: using scrollbars on a canvas

I'm trying to make a canvas scrollable. However, once I try to set up scrollbars to work with the canvas, tkinter seems to completely ignore the dimensions I initially set for my canvas. I've tried packing them all in a frame, setting the canvas to…
33
votes
5 answers

How to create downloading progress bar in ttk?

I want to show a progress bar while downloading a file from the web using the urllib.urlretrive method. How do I use the ttk.Progressbar to do this task? Here is what I have done so far: from tkinter import ttk from tkinter import * root = Tk() pb…
Hanix
  • 333
  • 1
  • 4
  • 4
33
votes
1 answer

Getting every child widget of a Tkinter window

Is it possible to get all of the children of a Tkinter widget, then get the children's children etc.? Basically I want all of the widgets within one entire window. Edit : I found a solution utilizing Bryan's line : def all_children (wid) : …
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
33
votes
2 answers

How to embed a terminal in a Tkinter application?

I want to embed a terminal in my main Tkinter window. I would like to have a sub window where a terminal (Bash based terminal) would run. I would like also to be able to let my program interact with the terminal, at least I would like to read the…
alessandro
  • 3,838
  • 8
  • 40
  • 59
33
votes
3 answers

When to use pack or grid layouts in tkinter?

Are there any best practice tips regarding when one should use pack vs. grid for their layouts? From what I've been reading via google, the concencus seems to be that grid can handle any pack scenario but not vice-versa. To start the conversation,…
Malcolm
  • 5,125
  • 10
  • 52
  • 75
33
votes
4 answers

How do I run unittest on a Tkinter app?

I've just begun learning about TDD, and I'm developing a program using a Tkinter GUI. The only problem is that once the .mainloop() method is called, the test suite hangs until the window is closed. Here is an example of my code: # server.py import…
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
33
votes
3 answers

Using buttons in Tkinter to navigate to different pages of the application?

I have quite a simple question here. In Tkinter (python), I was wondering who to use a button to go to different pages of my application, e.g a register page, and a login page. I am aware that GUI does not have 'pages' like websites do, I've seen a…
Caspar Wylie
  • 2,818
  • 3
  • 18
  • 32
33
votes
2 answers

How do I change button size in Python?

I am doing a simple project in school and I need to make six different buttons to click on. The buttons must have different sizes, but I can't find how do do it. I have made the button by using: def __init__(self, master): …
John Forsgren
  • 353
  • 1
  • 3
  • 4
33
votes
4 answers

How to attach a Scrollbar to a Text widget?

I am trying to attach a scrollbar to my Text field and have been unable to do so. Here is the segment of code: self.scroller = Scrollbar(self.root) self.scroller.place(x=706, y=121) self.outputArea = Text(self.root, height=26, width=100)…
jwebster
  • 628
  • 2
  • 10
  • 20
32
votes
6 answers

Python Logging to Tkinter Text Widget

Does any one out there have an example of how to setup logging in Python to a Tkinter Text Widget? I have seen this used in several apps but cannot figure out how to direct the logging to anything other than a log file.
user1214192
31
votes
2 answers

Disable / Enable Button in TKinter

I'm trying to make a button like a switch, so if I click the disable button it will disable the "Button" (that works). And if I press it again, it will enable it again. I tried things like if, else but didn't get it to work. Here's an example: from…
rattionline
  • 449
  • 1
  • 4
  • 9
31
votes
10 answers

cannot import name 'ImageTK' - python 3.5

I am trying to load in an image from the same folder as the one my python script is in. # create a class called Person # create init method # 2 attributes (name, and birthdate) # create an object from the Person class from PIL import Image,…
Cody Caro
  • 339
  • 1
  • 3
  • 5