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
24
votes
4 answers

Function to close the window in Tkinter

import tkinter class App(): def __init__(self): self.root = Tkinter.Tk() button = Tkinter.Button(self.root, text = 'root quit', command=self.quit) button.pack() self.root.mainloop() def quit(self): …
DRdr
  • 1,171
  • 3
  • 12
  • 12
24
votes
1 answer

How can I disable typing in a ttk.Combobox tkinter?

I have a Combobox that I can currently type into. I want it to be so that the user can only choose a valid option from the drop down. I can't seem to find a similar question online, and I don't see anything in the documentation that could help me…
tristan957
  • 551
  • 2
  • 4
  • 14
24
votes
3 answers

Resizing pictures in PIL in Tkinter

I'm currently using PIL to display images in Tkinter. I'd like to temporarily resize these images so that they can be viewed more easily. How can I go about this? Snippet: self.pw.pic =…
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
24
votes
2 answers

Tkinter custom create buttons

Can tkinter create custom buttons from an image or icon like this?
bunyaminkirmizi
  • 540
  • 2
  • 6
  • 22
24
votes
1 answer

Running matplotlib in tkinter

I have this beautiful sphere I made in matplotlib. How would I go about putting it in a tkinter frame widget? It'd be nice to be able to integrate it into an existing tkinter GUI. Also is it possible to rid of the menu bar below the display? I have…
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
24
votes
5 answers

Python embeddable zip: install Tkinter

Python embeddable zip comes without pip and Tkinter. It is easy to install pip with get-pip.py in the embeddable zip. How can we install Tkinter too (assuming we do not have an existing Python installation in the same machine)?
antonio
  • 10,629
  • 13
  • 68
  • 136
24
votes
4 answers

How to horizontally center a widget using grid()?

I am using grid() to place widgets in a tkinter window. I am trying to put a label on the horizontal center of a window and have it stay there, even if the window is resized. How could I go about doing this? I don't want to use pack(), by the way. I…
L4undry
  • 281
  • 1
  • 4
  • 16
24
votes
9 answers

Transparent background in a Tkinter window

Is there a way to create a "Loading Screen" in Python 3.x using Tkinter? I mean like the loading screen for Adobe Photoshop, with transparency and so on. I managed to get rid of the frame border already using: root.overrideredirect(1) But if I do…
forumfresser
  • 461
  • 2
  • 6
  • 15
24
votes
1 answer

How to delete all children elements?

I am writing a GUI-based program using Python's tkinter library. I am facing a problem: I need to delete all children elements (without deleting a parent element, which in my case is colorsFrame). My code: infoFrame = Frame(toolsFrame, height = 50,…
Roman Nazarkin
  • 2,209
  • 5
  • 23
  • 44
24
votes
4 answers

How to change the color of certain words in the tkinter text widget?

I have a program that I want to be like the Python shell and change color of certain words when they are typed. Any help?
Dan Alexander
  • 2,004
  • 6
  • 24
  • 34
24
votes
4 answers

Displaying Matplotlib Navigation Toolbar in Tkinter via grid

I'm developing a small Tkinter GUI to draw matplotlib-plots. (It contains a few Entries and assembles the plot according to their content.) I have designed my plotting widget according to…
CodingCat
  • 4,999
  • 10
  • 37
  • 59
23
votes
2 answers

How to implement the MVC-pattern in Tkinter

I need a basic example where MVC pattern is used with Python TK. I have some code using Tkinter, however I would like to transform it using the MVC pattern.
user618677
  • 4,909
  • 6
  • 23
  • 24
23
votes
2 answers

How to select at the same time from two Listbox?

from Tkinter import * master = Tk() listbox = Listbox(master) listbox.pack() listbox.insert(END, "a list entry") for item in ["one", "two", "three", "four"]: listbox.insert(END, item) listbox2 =…
directedition
  • 11,145
  • 18
  • 58
  • 79
23
votes
3 answers

Make a Label Bold Tkinter

How do I make a Label in Tkinter Bold ? This is my code labelPryProt=Label(frame1,text="TEXTTEXT") labelPryProt.pack(side=LEFT,fill=BOTH,expand=False) labelPryProt.configure(font=("Helvetica",BOLD, 18))#this is not working not making the text as…
zzz123
  • 341
  • 1
  • 2
  • 7
23
votes
5 answers

Tkinter GUI with progress bar

I have a simple Tk GUI and a long process in a function attached to a button. I want a progress bar when I click on the button, just like it starts a long process. How can I do that? This is my current code: from tkinter import Button, Tk,…
j666
  • 349
  • 1
  • 2
  • 9