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

How can I prevent a window from being resized with tkinter?

I have a program which creates a window where a message is displayed according to a check box. How can I make the window size constant when the message is displayed and the message is not displayed? from Tkinter import * class App: def…
mahes
  • 1,074
  • 3
  • 12
  • 20
67
votes
10 answers

Why does Python installed via Homebrew not include Tkinter

I've installed Python via Homebrew on my Mac. brew install python After that I checked my Python version as 2.7.11, then I tried to perform import Tkinter I got following error message: Traceback (most recent call last): File "", line 1,…
Daniel Chen
  • 1,933
  • 5
  • 24
  • 33
67
votes
6 answers

Changing the text on a label

I am having trouble with using a key binding to change the value of a label or any parameter. This is my code: from tkinter import* class MyGUI: def __init__(self): self.__mainWindow = Tk() #self.fram1 = Frame(self.__mainWindow) …
editate
  • 673
  • 1
  • 5
  • 4
67
votes
9 answers

How to delete Tkinter widgets from a window?

I have a list of tkinter widgets that I want to change dynamically. How to delete the widgets from the window?
TheBeardedBerry
  • 1,636
  • 5
  • 20
  • 30
66
votes
10 answers

Does tkinter have a table widget?

I'm learning Python, and I would like to use it to create a simple GUI application, and since Tkinter is already built-in (and very simple to use) I would like to use it to build my application. I would like to make an app that will display a table…
Freewind
  • 193,756
  • 157
  • 432
  • 708
66
votes
6 answers

Unable to install tkinter with pyenv Pythons on MacOS

Versions of Python installed via pyenv fail to import tkinter: ※ python Python 3.8.1 (default, Feb 29 2020, 11:45:59) [Clang 11.0.0 (clang-1100.0.33.17)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import…
Carl G
  • 17,394
  • 14
  • 91
  • 115
66
votes
2 answers

How to set default text for a Tkinter Entry widget

How do I set the default text for a Tkinter Entry widget in the constructor? I checked the documentation, but I do not see a something like a "string=" option to set in the constructor? There is a similar answer out there for using tables and…
Big Al
  • 980
  • 1
  • 8
  • 20
65
votes
3 answers

How to find out the current widget size in tkinter?

I'm using Python and Tkinter, and I need to know the current dimensions (width, height) of a widget. I've tried somewidget["width"], but it returns only a fixed value, and is not updated whenever the widget size changes (e.g. when the window is…
Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
65
votes
10 answers

tkinter: binding mousewheel to scrollbar

I have this scroll-able frame (frame inside canvas actually). import Tkinter as tk class Scrollbarframe(): def __init__(self, parent,xsize,ysize,xcod,ycod): def ScrollAll(event): …
Chris Aung
  • 9,152
  • 33
  • 82
  • 127
64
votes
5 answers

Background color for Tk in Python

I'm writing a slideshow program with Tkinter, but I don't know how to change the background color to black instead of the standard light gray. How can this be done? import os, sys import Tkinter import Image, ImageTk import time root =…
olofom
  • 6,233
  • 11
  • 37
  • 50
64
votes
3 answers

Is there a GUI design app for the Tkinter / grid geometry?

Does anyone know of a GUI design app that lets you choose/drag/drop the widgets, and then turn that layout into Python code with the appropriate Tkinter calls & arrangement using the grid geometry manager? So far I've turned up a couple of pretty…
JDM
  • 1,709
  • 3
  • 25
  • 48
64
votes
3 answers

How to select a directory and store the location using tkinter in Python

I am creating a GUI with a browse button which I only want to return the path. I've been looking at solutions using code like below. Tkinter.Button(subframe, text = "Browse", command = self.loadtemplate, width = 10).pack() def…
Brad Conyers
  • 1,161
  • 4
  • 15
  • 24
64
votes
4 answers

tkinter creating buttons in for loop passing command arguments

I am trying to create buttons in tkinter within a for loop. And with each loop pass the i count value out as an argument in the command value. So when the function is called from the command value I can tell which button was pressed and act…
Marcel
  • 649
  • 1
  • 6
  • 3
63
votes
14 answers

Why isn't .ico file defined when setting window's icon?

When I tried to change the window icon in the top left corner from the ugly red "TK" to my own favicon using the code below, Python threw an error: from tkinter import * root = Tk() #some buttons, widgets, a lot of…
CrushedPixel
  • 1,152
  • 2
  • 13
  • 26
63
votes
4 answers

How do I bind the enter key to a function in tkinter?

I am a Python beginning self-learner, running on MacOS. I'm making a program with a text parser GUI in tkinter, where you type a command in a Entry widget, and hit a Button widget, which triggers my parse() funct, ect, printing the results to a Text…
Ghosty
  • 763
  • 1
  • 5
  • 10