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
26
votes
3 answers

How to know all style options of a ttk widget?

I am without knowledge of tck/tk. I have done carefully search on the internet but haven't found a good solution. For example, I created a LabelFrame using: import tkinter as tk from tkinter import ttk newBT = ttk.LabelFrame(width=100,…
Nan Zhou
  • 1,205
  • 1
  • 13
  • 14
26
votes
5 answers

Image on a button

I expect the same output for both of the scripts below. But I don't get the image on the button when I execute Script 1. However, Script 2 works well. Script 1 from Tkinter import * class fe: def __init__(self,master): …
coder
  • 1,901
  • 5
  • 29
  • 44
26
votes
3 answers

Tkinter dropdown Menu with keyboard shortcuts?

I would like to have a Dropdown Menu in Tkinter, that includes the shortcut key associated with this command. Is this possible? How would I also add the underline under a certain character, to allow for Alt-F-S (File->Save)?
skeggse
  • 6,103
  • 11
  • 57
  • 81
26
votes
1 answer

How to get the coordinates of an object in a tkinter canvas?

I can't seem to figure out how to retrieve the x,y position of an oval created on a Tkinter canvas using Python via c.create_oval(x0, y0, x1, y2) I understand that Tkinter creates the oval inside the box specified by x0,y0,x1,y2 and if I can get…
Omar
  • 39,496
  • 45
  • 145
  • 213
26
votes
1 answer

Tkinter OpenGL context in Python

I need to create an OpenGL context in Tkinker, for using it with PyOpenGL Python module. Tkinker doesn't natively support OpenGL context, but I found this page on PyOpenGL docs, explaining how to use a wrapper included in the module for…
FeliceMente
  • 263
  • 1
  • 3
  • 8
26
votes
8 answers

tkinter TclError: error reading bitmap file

I am trying to set an application icon (python3 / tkinter) like this: Interface() root.title("Quicklist Editor") root.iconbitmap('@/home/jacob/.icons/qle_icon.ico') root.resizable(0, 0) root.mainloop() no matter what I do, I keep getting an error…
Jacob Vlijm
  • 3,099
  • 1
  • 21
  • 37
25
votes
4 answers

Add multiple extensions in one filetypes mac - tkinter/filedialog/askopenfilename

I am trying to use tkinter.filedialog to choose the file in Mac. It works fine with one filetype in this way: filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx")]) However, I want to choose either xlsx or xls files. I searched and found…
Joe
  • 365
  • 1
  • 3
  • 8
25
votes
3 answers

Get Tkinter Window Size

How do I get the width and height of a Tkinter window?
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
25
votes
4 answers

Command for clicking on the items of a Tkinter Treeview widget?

I'm creating a GUI with Tkinter, and a major part of the GUI is two Treeview objects. I need the contents of the Treeview objects to change when an item (i.e. a directory) is clicked twice. If Treeview items were buttons, I'd just be able to set…
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
25
votes
2 answers

What are the arguments to Tkinter variable trace method callbacks?

Python has classes for Tkinter variables StringVar(), BooleanVar(), etc. These all share the methods get(), set(string), and trace(mode, callback). The callback function passed as the second argument to trace(mode, callback) is passed four…
Marshall Davis
  • 3,337
  • 5
  • 39
  • 47
25
votes
3 answers

problems importing ttk from tkinter in python 2.7

I'm working with an example file in a tutorial that asks me to first do two imports: from tkinter import * from tkinter import ttk I get an error. I researched a bit and found that in python 2.7.x I need to capitalize the 't'in tkinter, so I change…
Benjamin Boyce
  • 305
  • 1
  • 4
  • 11
25
votes
3 answers

How do I insert a JPEG image into a python Tkinter window?

How do I insert a JPEG image into a Python 2.7 Tkinter window? What is wrong with the following code? The image is called Aaron.jpg. #!/usr/bin/python import Image import Tkinter window =…
Aaron Esau
  • 1,083
  • 3
  • 15
  • 31
25
votes
4 answers

Embedding a Pygame window into a Tkinter or WxPython frame

A friend and I are making a game in pygame. We would like to have a pygame window embedded into a tkinter or WxPython frame, so that we can include text input, buttons, and dropdown menus that are supported by WX or Tkinter. I have scoured the…
user2961646
25
votes
3 answers

Python not configured for Tk

I am using Ubuntu 13.10 and Python 3.3.4 using pyenv. I have no problem using other modules. When I enter python console and write import tkinter It outputs Traceback (most recent call last): File "", line 1, in File…
serdar
  • 974
  • 2
  • 16
  • 24
25
votes
1 answer

How to update Tcl/Tk in Python?

Tcl and Tk in their version 8.6 have been out now for about six weeks. However, the files that can be downloaded from Tcl have a different folder structure and lack some files such as tk85.lib (or tk86.lib now) compared to the Tcl folder in…
Michael Westwort
  • 914
  • 12
  • 24