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
1 answer

Select a cell in tkinter.treeview and get the cell data

I would like to use a mouse pointer to select a cell in a table created by tkinter.treeview() and print out the cell's value. How can I do this? Below is a sample code to create a tkinter.treeview table. In this case, I would like to use my mouse…
Sun Bear
  • 7,594
  • 11
  • 56
  • 102
5
votes
1 answer

Progressbar with Percentage Label?

How can I put a label in the middle of a progressbar that shows the percentage? The problem is that python doesn't support transparency for label backgrounds, so I don't know how I can solve that.
Kyu96
  • 1,159
  • 2
  • 17
  • 35
5
votes
1 answer

cxfreeze widget with tkinter xgboost not showing but no error

My program is working in anaconda spyder. however, after freezing it all widgets that use the tkinter module work except for the widget with xgboost and pandas. No error showed, the build worked but the button is not working and not showing the…
jp-tech
  • 91
  • 7
5
votes
1 answer

Matplotlib bar chart- some bars are not visible and seem to be of different width

The problem is connected with the matplotlib bar chart. To be specific, when I draw a bar chart some of the bars are not visible. Dots and bars uses the same data series so should be in the same places . I do not know what is the source of this…
Mariusz Ch.
  • 51
  • 1
  • 5
5
votes
1 answer

How to emit events in tkinter?

I have simple app written in python3 and tkinter module. I want to write my custom widget and need to send my custom event. Why this sample code below does not work? #!/usr/bin/env python3 from tkinter import * class MyWidget(Listbox): def…
BPS
  • 1,133
  • 1
  • 17
  • 38
5
votes
1 answer

Changing the colour of text automatically inserted into tkinter widget

I have a text box widget that has three messages inserted into it. One is a start message, one an ending message, and one a message to alert when a 'unit' has been destroyed. I want the starting and ending messages to be black, but the destoyed…
Alice
  • 588
  • 1
  • 8
  • 25
5
votes
1 answer

No way to color the border of a Tkinter Button?

It works with some other widgets, but not with Buttons. from Tkinter import * root = Tk() root.geometry("600x300+400+50") btn_up = Button(root, text='Go UP') btn_up.config(highlightbackground="red", highlightcolor="red", highlightthickness=10,…
Maicon Erick
  • 127
  • 1
  • 11
5
votes
1 answer

python | tkinter and threading: "main thread is not in main loop"

I'm working on an "multilayered" GUI for my company to monitor temperature and status. Because I'm quite new to the python programming, I could use some help with my code. Quick code explanation: The code is structured by classes. The "Main" inits…
M.Michel
  • 51
  • 1
  • 1
  • 4
5
votes
2 answers

tkinter move object on canvas

I'm new in python. I'm trying to achieve a simple object movement on canvas. The idea is to simply update X, Y coordinates and redraw the oval. I've tried to use canvas.update() every time I update coordinates but it doesn't work this way. class…
user4540334
5
votes
4 answers

Python Module for rich gui interfaces

Was just wondering what all python modules are available for rich(and easy to build :) ) gui interfaces. I am aware of Tkinter and Pwm extension but any other reference would be highly appreciated.
SDK
  • 51
  • 1
  • 4
5
votes
1 answer

Tkinter image is blank

I have the following python code: from tkinter import * from PIL import ImageTk, Image import sys, os height = 5 width = 8 # window = Tk() class NSUI(Frame): def reload(self): os.execl(sys.executable, sys.executable, *sys.argv) …
Kevin.a
  • 4,094
  • 8
  • 46
  • 82
5
votes
1 answer

Python Tkinter side notebook tabs

I am redesigning the GUI of a program that uses tkinter in python. I used ttk widgets for this program, but I think, even on W10, the interface is way too old so I decided to update the visual interface for the program using METRO interface or W10…
5
votes
4 answers

Getting a value from Tkinter OptionMenu in Python

I'm trying to print out the value selected in the option menu but only the first value gets printed everytime I run the code, even if I change my selection to b or c. Not sure where I'm wrong.This is my code: from tkinter import…
West
  • 2,350
  • 5
  • 31
  • 67
5
votes
3 answers

Python tkinter PhotoImage not working correctly

I'm trying to use tkinter but this code doesn't work and I'm wondering if anyone knows why thanks. from tkinter import * window = Tk() window.title("tkinter stuff") photo1 = PhotoImage("file=hs.gif") Label(window,…
Daniel
  • 69
  • 1
  • 2
5
votes
7 answers

How should I launch a Portable Python Tkinter application on Windows without ugliness?

I've written a simple GUI program in python using Tkinter. Let's call this program 'gui.py'. My users run 'gui.py' on Windows machines from a USB key using Portable Python; installing anything on the host machine is undesirable. I'd like my users to…
Andrew
  • 2,842
  • 5
  • 31
  • 49