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
2 answers

How big is too big for a Pyinstaller exe file?

Apologies for the extremely vague question, but I've been wrecking my head trying to reduce the size of an Pyinstaller .exe and I decided to check what more experienced Python developers think about the general file size of such files. I'm working…
ISquared
  • 364
  • 4
  • 22
5
votes
2 answers

tkinter - Why is there such a thing like bbox?

Now that I'm working more with tkinter Canvas I was wondering about the use of bbox. For me I'm using bbox to get the coords of an element but the Canvas already have a method to return the coords of an item. So why did they invent something like…
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
5
votes
1 answer

How to detect multiple screens

So you know when you connect a computer to a monitor and click extend display you get two displays and if you have another one you would have 3 different display screens... So how do I create a tkinter program that detects multiple display screens…
5
votes
1 answer

Python3 and Tkinter - Unable to alloc 27 bytes

In a piece of my code, my script generates a string to copy it in a scrolled text widget. if the size of the string is not so heavy, this process works without any kind of issues, but when the string is heavy, the script can't paste it into the…
5
votes
2 answers

What is the best way to stop a thread and avoid 'RuntimeError' in python using threading and tkinter modules?

I have went through multiple solutions on the net, but they require a lot of code that might get confusing once you scale up. Is there a simple way to stop the thread and avoid the RuntimeError: threads can only be started once, in order to call the…
5
votes
4 answers

Exception in Tkinter callback using SHAP

I'm trying to draw some SHAP plots in Python to gain a deeper understanding of the output of my machine learning models. This is the method I'm calling in a for loop: def plotAndSaveSHAPSummary(model,train_data,x_train,pathToSHAPPlots): …
Hagbard
  • 3,430
  • 5
  • 28
  • 64
5
votes
3 answers

How to move popup window when scrolling a tkinter treeview?

I have a tkinter treeview with a vertical scrollbar. To make it (look like) editable, I create a popup Entry when the user double-clicks on a cell of the treeview. However, I can't make the popup to move when the treeview is scrolled. import tkinter…
Jakaria
  • 197
  • 1
  • 7
5
votes
2 answers

Working on a way to return the text of a button after the button is clicked in tkinter

I'm trying to create a list of buttons that are clicked with this lambda function: button1.config(command=(lambda x: (clicked.append(x)))(button1.cget("text"))) It seems to sort of work but it prints the button text immediately i.e. it doesn't wait…
user637965
5
votes
1 answer

tkinter ttk style Layout not found

Getting an error on the following code. Keep looking at examples and re-evaluating code but cannot really spot any reason this shouldn't run. Is there anything incorrect such that the 'mRRed' layout cannot be found? (And I'm sure the…
Chemistpp
  • 2,006
  • 2
  • 28
  • 48
5
votes
1 answer

Buttons have their own coordinate system according to the "grid_location" method?

I'm trying to use the grid_location method, from the Grid Geometry Manager, in Tkinter, but it seems that I'm doing something wrong. Here's my code: from tkinter import * root = Tk() b=Button(root, text="00") b.grid(row=0,…
Marcos Saito
  • 530
  • 2
  • 10
  • 17
5
votes
1 answer

Python 3- How to retrieve an image from the web and display in a GUI using TKINTER?

I want a function that, when a button is clicked, it will take an image from the web using URLLIB and display it in a GUI using TKINTER. I'm new to both URLLIB and TKINTER, so I'm having an incredibly difficult time doing this. Tried this, but it…
Parseltongue
  • 11,157
  • 30
  • 95
  • 160
5
votes
3 answers

Is there a way to change height of tkinter Treeview heading?

I got a problem with changing the height of the Treeview.heading. I have found some answers about the dimensions of Treeview.column, but when I access Treeview.heading in the documentation, there is not a single word about changing the height of the…
Sqoshu
  • 944
  • 2
  • 9
  • 16
5
votes
1 answer

Tkinter button different commands on right and left mouse click

I am making minesweeper game in Python and use tkinter library to create gui. Is there any way to bind to tkinter Button two commands, one when button is right-clicked and another when it's left clicked?
5
votes
1 answer

how to create a GUI using python to take user input and insert it into excel sheet

I want to create a python GUI with one user input which will be inserted to an excel sheet whenever the user Enters insert button, and another button called e.g Show words, which will read all the words which are inserted into the excel sheet, any…
programming freak
  • 859
  • 5
  • 14
  • 34
5
votes
4 answers

Python Tkinter Font Chooser

I'm trying to write simple notepad with Tkinter. And I need some font chooser. So my question is: is there included one? and if there isn't, where can I get one? Thanks.
SaulTigh
  • 913
  • 2
  • 14
  • 27