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

How to bind a click event to a Canvas in Tkinter?

I was just wondering if there was any possible way to bind a click event to a canvas using Tkinter. I would like to be able to click anywhere on a canvas and have an object move to it. I am able to make the motion, but I have not found a way to…
lukejano
  • 301
  • 1
  • 4
  • 12
23
votes
6 answers

calling a python script on button click using python and tkinter

I have a python script which has the functionality of sending an email to a user. I executed this script and it is working fine. In another python script I have only a button, so when I click on this button I want the other python script which…
Valla
  • 2,414
  • 11
  • 42
  • 73
23
votes
5 answers

Tkinter main window focus

I have the following code window = Tk() window.lift() window.attributes("-topmost", True) This code works in that it displays my Tkinter window above all other windows, but it still only solves half of the problem. While the window is in fact…
Python Spoils You
  • 374
  • 1
  • 2
  • 12
23
votes
1 answer

Save File Dialog in Tkinter

I am implementing a GUI based text editor in python. I have displayed the text area but when I try to use the asksaveasfile method in Tkinter, it shows that the file has been saved but when I try and open the same file in my desktop editor, it gives…
Rohit Shinde
  • 1,575
  • 5
  • 21
  • 47
23
votes
4 answers

How do I compile my Python 3 app to an .exe?

How do I convert my Python app to a .exe? I made a program with tkinter and was wondering how to make it possible for others to use. I use Python 3.3. I searched for a bit but could not find anything.
Sam Chahine
  • 530
  • 1
  • 17
  • 52
23
votes
3 answers

Difference between import tkinter as tk and from tkinter import

I know it is a stupid question but I am just starting to learn python and i don't have good knowledge of python. My question is what is the difference between from Tkinter import * and import Tkinter as tk ?Why can't i just write import…
Chris Aung
  • 9,152
  • 33
  • 82
  • 127
23
votes
3 answers

simple graphics for python

I am making a simulation in python that needs visualization. I am looking for the most simple graphics library python has. Is there anything that can let me do something along the lines of: setWindow(10,10) setCube(0,0,1,1,1)#windowX, windowY,…
EasilyBaffled
  • 3,822
  • 10
  • 50
  • 87
23
votes
5 answers

python tkinter with a simple web wrapper

I am writing a simple python program with a UI, and i can't find out how to put a web page inside the program. I tried searching on Google, but i can't find any simple implementations. Any links would be great. Just to recap, i just want a simple…
user1310420
23
votes
1 answer

python ttk.Entry how to center the input

Here is a screenshot of a program I am writing using Python Tkinter. I use ttk.Entry widget to get the user input. I would like to know how to center the input in the Entry (as you can see, the input is now stuck to the left part of the widget)
user12345
  • 407
  • 1
  • 3
  • 10
23
votes
2 answers

python tkinter: how to work with pixels?

using google (and this site) i have seen some similar questions but my problem is still here: "i want to draw an image (without reading a file) , being able to manipulate every single pixel's colour in that image." i have seen another question where…
Alberto Perrella
  • 1,318
  • 5
  • 12
  • 19
22
votes
2 answers

Tkinter binding a function with arguments to a widget

I have a tkinter frame and a button attached to it: from tkinter import * def rand_func(a,b,c,effects): print (a+b+c) root=Tk() frame=Frame(root) frame.bind("",lambda a=10, b=20, c=30:…
Serban Razvan
  • 4,250
  • 3
  • 21
  • 22
22
votes
3 answers

tkinter in Ubuntu inside Windows 10. Error: "no display name and no $DISPLAY environment variable"

I have recently installed the Ubuntu app for Windows 10 so that I can use it for a class. I am following some cryptic assignment directions to "try out tkinter". I installed the package python3-tk in the Ubuntu terminal window following instructions…
Zach
  • 627
  • 1
  • 5
  • 11
22
votes
11 answers

Tkinter error: Couldn't recognize data in image file

I'm trying to put a jpg image to a tkinter canvas. tkinter gives me this error: couldn't recognize data in image file I use the code from the documentation: canv = Canvas(root, width=80, height=80, bg='white') canv.grid(row=2, column=3) img =…
Igor234
  • 359
  • 1
  • 3
  • 11
22
votes
3 answers

Why is the Tkinter render quality much worse on windows?

I have developed a python app with Tkinter on a Mac. It involves forms, and canvas drawings. On the Mac, it looks great. However on my Dell laptop (4K display, and more powerful than my Mac), the Tkinter ui appears very pixelated and certain…
Caspar Wylie
  • 2,818
  • 3
  • 18
  • 32
22
votes
6 answers

Install tkinter in python3.6 on Ubuntu

+--------+-----------------------------------+ | OS | Ubuntu 12.04 | +--------+-----------------------------------+ | Python | 2.7, 3.2 and source installed 3.6 | +--------+-----------------------------------+ Since there…
lapin
  • 432
  • 1
  • 5
  • 11