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
37
votes
9 answers

TKinter in a Virtualenv

Trying to run python code with TKinter-based widgets from a virtualenv. user@computer:~/myproject$ env/bin/python Python 2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more…
Jasper van den Bosch
  • 3,169
  • 4
  • 32
  • 55
37
votes
2 answers

How to modify the default font in Tkinter?

I'm working on a GUI in Python2.7, with Tkinter, and I have an annoying problem. I would like to define the default font used by all the widgets, if possible in one line. This line modify only the font used in Entry, or…
ericc
  • 741
  • 3
  • 8
  • 19
36
votes
5 answers

Image resize under PhotoImage

I need to resize an image, but I want to avoid PIL, since I cannot make it work under OS X - don't ask me why... Anyway since I am satisfied with gif/pgm/ppm, the PhotoImage class is ok for me: photoImg =…
alessandro
  • 3,838
  • 8
  • 40
  • 59
36
votes
11 answers

How to add placeholder to an Entry in tkinter?

I have created a login window in tkinter which has two Entry field, first one is Username and second one is Password. code from tkinter import * ui = Tk() e1 = Entry(ui) #i need a placeholder "Username" in the above entry…
Pierce
  • 373
  • 2
  • 4
  • 6
36
votes
3 answers

Update Tkinter Label from variable

I wrote a Python script that does some task to generate, and then keep changing some text stored as a string variable. This works, and I can print the string each time it gets changed. I can get the Label to display the string for the first time,…
Tom
  • 395
  • 1
  • 3
  • 7
36
votes
7 answers

Difference between tkinter and Tkinter

When I answer Tkinter questions I usually try and run the code myself, but sometimes I get this error: Traceback (most recent call last): File "C:\Python27\pygame2.py", line 1, in from tkinter import * ImportError: No module named…
Serial
  • 7,925
  • 13
  • 52
  • 71
36
votes
10 answers

Specify the dimensions of a Tkinter text box in pixels

How would I specify the dimensions of a Tkinter text box using pixels? I am not trying to change the font size, I am using this to help me scale it to the size of the window.
xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60
36
votes
4 answers

tkinter app adding a right click context menu?

I have a python-tkinter gui app that I've been trying to find some way to add in some functionality. I was hoping there would be a way to right-click on an item in the app's listbox area and bring up a context menu. Is tkinter able to accomplish…
tijko
  • 7,599
  • 11
  • 44
  • 64
35
votes
3 answers

How to set the min and max height or width of a Frame?

The size of Tkinter windows can be controlled via the following methods: .minsize() .maxsize() .resizable() Are there equivalent ways to control the size of Tkinter or ttk Frames? @Bryan: I changed your frame1.pack code to the…
Malcolm
  • 5,125
  • 10
  • 52
  • 75
35
votes
5 answers

Passing data between separately running Python scripts

If I have a python script running (with full Tkinter GUI and everything) and I want to pass the live data it is gathering (stored internally in arrays and such) to another python script, what would be the best way of doing that? I cannot simply…
Jordan Gleeson
  • 531
  • 1
  • 4
  • 12
35
votes
2 answers

How to set border color of certain Tkinter widgets?

I'm trying to change the background color of my Tkinter app, but for certain widgets it leaves a white border around the edges. For example, this: from tkinter import * COLOR = "black" root = Tk() root.config(bg=COLOR) button =…
jefdaj
  • 2,025
  • 2
  • 21
  • 33
35
votes
2 answers

How to add space between two widgets placed in grid in tkinter ~ python?

a. Have placed a widget in the row 0 in the grid as shown below. self.a_button = Button(root, text="A Button") self.a_button.grid(row=0, column=1) b. And tried placing another widget in row 2 inside the grid. self.b_button = Button(root, text="B…
Vimo
  • 1,061
  • 3
  • 12
  • 22
35
votes
4 answers

Execute a command on Remote Machine in Python

I am writing a program in python on Ubuntu, to execute a command ls -l on RaspberryPi, connect with Network. Can anybody guide me on how do I do that?
Irfan Ghaffar7
  • 1,143
  • 4
  • 11
  • 30
34
votes
12 answers

ImportError DLL load failed importing _tkinter

I'm using python 2.7.2 and windows 7. I searched through internet, helps and other sources but i can't find an answer to my problem. One of my source imports tkinter, and this one imports _tkinter. At this moment it say ImportError DLL load failed:…
paduraru2009
  • 595
  • 2
  • 5
  • 11
34
votes
4 answers

Tkinter canvas zoom + move/pan

Tkinter's canvas widget has built-in features to: move/pan the canvas (for example with Click + Drag) with canvas.scan_mark and canvas.scan_dragto, see this question zoom the vector elements on the canvas with canvas.scale, but sadly, this doesn't…
Basj
  • 41,386
  • 99
  • 383
  • 673