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

How to make tkinter frames in a loop and update object values?

I have a class called Bones I have 5 Bones in my skeleton dictionary. However in my actual implementation there are 300+ bones, that's why I am asking this question today on stackoverflow. Each Bone has: ID: An int to identify a bone w: w position…
Khloe
  • 157
  • 1
  • 2
  • 6
5
votes
1 answer

Tkinter how to change the color of treeview selected items

How does one go about changing the selected text color in treeview, I can't seem to find much on the subject. Here is what I have tried but the color doesn't change to red as I would like, it stays blue. from tkinter import * from tkinter.ttk import…
Daniel Huckson
  • 1,157
  • 1
  • 13
  • 35
5
votes
1 answer

Is tkinter's `after` method thread-safe?

Since tkinter isn't thread-safe, I often see people use the after method to queue some code for execution in the main thread. Here's an example: import tkinter as tk from threading import Thread def change_title(): root.after(0, root.title,…
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
5
votes
3 answers

Why subplots are not taking all space allowed by figure in Matplotlib embedded with Tkinter?

I want to show a lot of subplots in a Tkinter window and be able to scrolldown to see all the plots with a good size. However, it is all packed and it seems that the subplots don't take all the space allowed in my figure and only restricts to the…
5
votes
1 answer

Why tkinter fails on Mac

I was using tkinter on Mac. But when I used the following simple code, the computer will restart. import tkinter as tk window = tk.Tk() What is the problem? Thank you! Mac: Mojave, version 10.14.6 tkinter: version 8.6 python: 3.7.3
Shark Deng
  • 960
  • 9
  • 26
5
votes
2 answers

how to fix "TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'"

I am trying to create a python file that will spam a directory with .txt files. I decided to start working with Tkinter but whenever I try to input a number I get this error message "TypeError: int() argument must be a string, a bytes-like object or…
Lazer
  • 73
  • 1
  • 1
  • 8
5
votes
3 answers

How to change ttk button background and foreground when hover it in tkinter

I'm trying to change ttk.tkinter button background to black and foreground colour to white when mouse is hover it. Have tried highlightbackground and activebackground but doesn't yield the result I'm looking for. import tkinter as tk import…
O JOE
  • 587
  • 2
  • 17
  • 31
5
votes
1 answer

super() in tkinter application

I am having trouble understanding this error. In the below code,when I use tk.Frame everything works as intended. However if I use super(), I get thrown an AttributeError ('Application object has no attribute tk'). class Application(tk.Frame): …
Rahul
  • 177
  • 3
  • 9
5
votes
3 answers

Python : Tkinter widget background (buttons, entries etc)

I'm having a bit of a problem in creating my GUI's. It's when changing backgrounds, I can change the whole background, and also the background of the labels and such, but when it comes to buttons and entry fields etc, there is a white area behind…
Tehnix
  • 2,020
  • 2
  • 18
  • 23
5
votes
3 answers

python : tkinter treeview colors are not updating

This is my first post, please excuse me if I made mistake in the format, I will gladly change it if required. I'm creating an interface for scientific datas analysis using Tkinter. For a list of molecules, four can be represented in separate plots.…
Gonzalez87
  • 162
  • 1
  • 9
5
votes
1 answer

Unable to exit tkinter app when using "wait_variable()"

I have a python code that includes tkinter window and other running tasks. I've been trying to bind "WM_DELETE_WINDOW" event to a function that exits my python code when I close the window but can't achieve that. This is what I try: def on_exit(): …
David Sidarous
  • 1,202
  • 1
  • 10
  • 25
5
votes
3 answers

Show Large Image using Scrollbar in Python

I would like to display an image using tkinter (Python 3). The image is very long. Therefore, I would like to add a vertical scrollbar. This is what I have tried: (based on this question: Scrollbars for a .jpg image on a Tkinter Canvas in…
henry
  • 875
  • 1
  • 18
  • 48
5
votes
4 answers

Tkinter Treeview issue when inserting rows with tags

I have just switched from python 3.6 to python 3.7. I have a function which inserts rows in a Treeview tree with tags. The tags are used for giving a foreground color and a background color to the rows inserted to the tree. My code was working ok…
Andreas P.
  • 71
  • 1
  • 3
5
votes
3 answers

.iconbitmap gives TclError: bitmap "*.ico" not defined

I am working with Tkinter and i keep getting an error message when trying to run the code, could someone help? from tkinter import * from tkinter.messagebox import showinfo def reply(name): showinfo(title='Reply', message='Hello %s!' %…
5
votes
1 answer

How to change which tkinter Button is highlighted via arrow keys?

I am working on a keyboard app using tkinter library in python. I have made a keyboard. What I want is to highlight a key and on clicking on arrow keys I want to change that highlighted key. Here is my code for keyboard. from tkinter import * import…
Jawad Malik
  • 608
  • 5
  • 21