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

Tkinter objects being garbage collected from the wrong thread

I seem to be breaking tkinter on linux by using some multi-threading. As far as I can see, I am managing to trigger a garbage collection on a thread which is not the main GUI thread. This is causing __del__ to be run on a tk.StringVar instance,…
Matthew Daws
  • 1,837
  • 1
  • 17
  • 26
5
votes
1 answer

Python: Z-index on tkinter

Does exist a way to specificate the depth of an element of the Tkinter canvas, like the HTML's z-index? Currently the only way I found to let the element overlap as I want is to create it in a specific order; the problem is that some element must be…
L'ultimo
  • 521
  • 1
  • 5
  • 17
5
votes
2 answers

TkInter Frame doesn't load if another function is called

I'm writing a Python programme which listens for RFID input and only runs if a valid token is presented. The programme also has a GUI which I'm wanting to build using TkInter. Both parts of the puzzle work fine in isolation, however as it stands I…
5
votes
1 answer

Integrate turtle module with tkinter canvas

I am trying to integrate the Turtle module into an interface I have created with TKInter, currently I have a canvas where I would like for the turtle to draw to (see example 1). However I am lost in how to get the draw to it.
E J
  • 81
  • 1
  • 1
  • 6
5
votes
4 answers

Getting the default font in Tkinter

I'm running Python 3.6 and was wondering if there is a way to get the default font that Tkinter uses, more specifically the default font that the Canvas object uses when canvas.create_text is called.
Ryan
  • 727
  • 2
  • 14
  • 31
5
votes
2 answers

How to align checkbutton in ttk to the left side

I want to configure stylestyle.configure('TCheckbutton', background=theme, foreground='white', anchor=tkinter.W) of tkinter.ttk.Checkbutton to align that checkbutton to the left side, because now it is in a center. Big thanks to every answer :)
Jakub Bláha
  • 1,491
  • 5
  • 22
  • 43
5
votes
1 answer

How to get the HWND of a Tkinter window on Windows?

How to get the HWND of a Tkinter window in python3 on windows? I would like to get the native window handle of the Tkinter window if possible. I would need either the HWND or HDC of the window to perform custom drawing operations. Is there a way…
Szabolcs Dombi
  • 5,493
  • 3
  • 39
  • 71
5
votes
3 answers

tkinter left clicks on a TAB in your GUI. How to detect when user has done this

I have a GUI written in tkinter and all works fine. I want to enhance it so that when a user left clicks on a certain tab with the mouse, a method is executed. I thought this would be straight forward but I can't get it working. My code is def…
Ab Bennett
  • 1,391
  • 17
  • 24
5
votes
2 answers

Is there an efficient way to tell if text is selected in a Tkinter Text widget?

On this page describing the Tkinter text widget, it's stated that 'The selection is a special tag named SEL (or “sel”) that corresponds to the current selection. You can use the constants SEL_FIRST and SEL_LAST to refer to the selection. If there’s…
Brandon
  • 3,684
  • 1
  • 18
  • 25
5
votes
1 answer

spreadsheet-like-app in tkinter?

Suppose one were to want to build a spread-sheet in tkinter. So one could have (many) cells, most of which would take text entry, but some could be drop-down lists (tkinter comboboxes) etc. Is that feasible in tkinter for a spreadsheet of any size?…
user3486991
  • 451
  • 6
  • 14
5
votes
1 answer

Tkinter - How to remove the border around a Frame?

I have a program with several frames. Everything works well however I cannot figure out why the border around one of the frames exist. I have tried a few things. Here is how my frame is created: kwListFrame = Frame(root) kwListFrame.grid(row = 1,…
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
5
votes
1 answer

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Trying to create a GUI using classes and I keep having problems with this error. I am unsure what it means as I only have one class as far as I can see, my error is: Traceback (most recent call last): File "C:/Users/Blaine/Desktop/Computing…
5
votes
2 answers

Changing image in tkinter canvas in while loop

Entirety of my code is here. Using tkinter's canvas, I am trying to create a small game that allows people to practice learning the notes on the treble clef. A random note is initially displayed and the user must select the correct note. I am unable…
5
votes
1 answer

How to find the path of Tcl/Tk library that Tkinter is currently using?

TCL_LIBRARY and TK_LIBRARY environment variables can be used to bind Tkinter with proper Tcl/Tk installation. How to get the location of Tcl/Tk from working Tkinter instance? (I'm running a frontend in non-virtual Python with working Tkinter and I…
Aivar
  • 6,814
  • 5
  • 46
  • 78
5
votes
1 answer

Need to call class method from different class without initialization of the first class or some other way around it

I have a small problem with my code. There are two classes. First one creates a window with a Options button. Upon clicking the button, the second class is called and creates another window with an Ok button. Let's say there is also a checkbox,…
Mike235
  • 51
  • 2
1 2 3
99
100