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
21
votes
3 answers

ImportError when importing Tkinter in Python

I'm trying to test GUI code using Python 3.2 with standard library Tkinter but I can't import the library. This is my test code: from Tkinter import * root = Tk() w = Label(root, text="Hello, world!") w.pack() root.mainloop() The shell reports…
carryall
  • 482
  • 3
  • 10
  • 21
21
votes
3 answers

Display Listbox with columns using Tkinter?

I'm trying to create a Listbox in Tkinter that has columns. I'm returning from a DB query records and would like to display each entry in it's own column for each record. Looking at Listbox, I feel like there should be this functionality there but…
themaestro
  • 13,750
  • 20
  • 56
  • 75
21
votes
2 answers

tkinter Canvas Scrollbar with Grid?

Relatively new to Tkinter and Python. So kindly bear with me. I am trying to display the following GUI and want to have a scrollbar in Frame2 to display only 5x5 buttons at a time. Looks like Tkinter Frames don't support scrollbar and hence added a…
user3300676
  • 307
  • 2
  • 3
  • 8
21
votes
2 answers

Tkinter Canvas creating rectangle

In python, tkinter, I'm trying to make a game that involves creating shapes onto a canvas. For example, I want a red rectangle to appear over my canvas image. When I execute my code, the rectangle you see is about 1 pixel in size, and I'm not sure…
Jake
  • 772
  • 2
  • 8
  • 18
21
votes
3 answers

tkinter Treeview: get selected item values

I'm just starting with a small tkinter tree program in python 3.4. I'm stuck with returning the first value of the row selected. I have multiple rows with 4 columns and I am calling a function on left-click on a item: tree.bind('',…
samtun
  • 434
  • 1
  • 3
  • 12
21
votes
2 answers

tkinter.messagebox.showinfo doesn't always work

I have just started working with Python's tkinter GUI tool. In my code I create an simple GUI with one button and I want to show the user a messagebox if they click on the button. Currently, I use the tkinter.messagebox.showinfo method for it. I…
v3nd3774
  • 213
  • 1
  • 2
  • 4
21
votes
1 answer

What's the difference between tkinter's Tk and Toplevel classes?

In Python 3, I run the following from the interactive shell: >>> import tkinter >>> type(tkinter.Tk()) >>> type(tkinter.Toplevel()) Both of these create individual windows. I assume that tkinter.Tk()…
Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
21
votes
1 answer

Tkinter AttributeError: object has no attribute 'tk'

I've looked around a bit, but I can't find an answer to my error. Here is the code: import tkinter as tk root=tk.Tk() class Page(tk.Frame): '''Enables switching between pages of a window.''' def __init__(self): self.widgets={} …
PlatypusVenom
  • 547
  • 2
  • 6
  • 12
21
votes
6 answers

How to change the color of ttk button

I am using Python 3.x on Windows. My problem is I want to customize a button widget of ttk by completely changing its background and foreground color. But so far, I have been unsuccessful. My desired button is: I read the ttk.Style guide and used…
maq
  • 1,175
  • 3
  • 17
  • 34
21
votes
2 answers

Threads and tkinter

I've heard that threads in Python are not easy to handle and they become more tangled with tkinter. I have the following problem. I have two classes, one for the GUI and another for an infinite process. First, I start the GUI class and then the…
Martin DLF
  • 231
  • 2
  • 3
  • 7
21
votes
2 answers

What does calling Tk() actually do?

I was brushing up on Tkinter when I looked upon a minimal example from the NMT Tkinter 8.5 Reference. #!/usr/bin/env python import tkinter as tk class Application(tk.Frame): def __init__(self, master=None): …
Doktoro Reichard
  • 577
  • 1
  • 7
  • 22
21
votes
1 answer

urllib.urlretrieve file python 3.3

I know I have seen the answer somewhere a couple of weeks ago but I can't find it now. Simple urllib.urlretrieve in Python 3.3. How do you do it? I'm trying to download an mp4/html(Page does not exist) scenario, either the page if it does not…
confused
  • 1,283
  • 6
  • 21
  • 37
21
votes
1 answer

What magic prevents Tkinter programs from blocking in interactive shell?

Note: This is somewhat a follow-up on the question: Tkinter - when do I need to call mainloop? Usually when using Tkinter, you call Tk.mainloop to run the event loop and ensure that events are properly processed and windows remain interactive…
poke
  • 369,085
  • 72
  • 557
  • 602
21
votes
3 answers

How to change font and size of buttons and frame in tkinter using python?

This is the code that i used to generate a simple text box and a button in tkinter. What should be the parameters to have a better look of the frame and buttons? root = Tk.Tk() def submit(): query = entry.get() retrieve(query) entry =…
Abkb
  • 271
  • 2
  • 3
  • 6
21
votes
2 answers

In Tkinter, How I disable Entry?

How I disable Entry in Tkinter. def com(): .... entryy=Entry() entryy.pack() button=Button(text="Enter!", command=com, font=(24)) button.pack(expand="yes", anchor="center") As I said How I disable Entry in com function?
Emek Kırarslan
  • 315
  • 1
  • 4
  • 11