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

lambda in for loop only takes last value

Problemset: Context Menu should show filter variables dynamically and execute a function with parameters defined inside the callback. Generic descriptions show properly, but function call is always executed with last set option. What I have…
R4PH43L
  • 2,122
  • 3
  • 18
  • 30
22
votes
3 answers

Is it possible to get widget settings in Tkinter?

It'd be awesome if I could get something like the below. Pseudo Code: U = widget1.SettingsGet() Print U Upon printing U something like this would be returned: widget1(background='green',foreground='grey',boarderwidth=10, relief='flat') It would…
rectangletangle
  • 50,393
  • 94
  • 205
  • 275
22
votes
1 answer

Tkinter askopenfilename() won't close

I'm using the following code snippet to open a file chooser dialog box. It opens up the dialog fine, but after a file is chosen the dialog box stays open for the duration of the execution of the rest of my code, which is 3-4 min. I thought…
mmmkay
  • 696
  • 1
  • 7
  • 12
22
votes
1 answer

"tkinter TclError: bad file type" using askopenfilename

This is my first time using Tkinter. I've imported it and it has been working up until this point. There's seems to be something wrong with the file type? I'm on a Mac as well if that makes any difference. Here's my code: def importTracks(self): …
terratunaz
  • 614
  • 3
  • 9
  • 19
22
votes
2 answers

Play Animations in GIF with Tkinter

I've been trying to play an animated gif using Tkinter.PhotoImage, but haven't been seeing any success. It displays the image, but not the animation. The following is my code: root = Tkinter.Tk() photo = Tkinter.PhotoImage(file =…
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
22
votes
3 answers

Expand Text widget to fill the entire parent Frame in Tkinter

I got this Text widget, and I'd like for it to expand and fill its entire parent, using the Grid geometry manager. According to the examples I've seen, this sample program should work, alas it doesn't, when expanding the window, the contents are not…
SoManyGoblins
  • 5,605
  • 8
  • 45
  • 65
22
votes
1 answer

Debug a problem in tk85.dll in an application that embeds the Python interpreter

My C++ application embeds the Python interpreter, but seems to be having some trouble when it shuts down. Right after the main window closes, I get a segmentation fault (this is Windows, but we'll call it a segmentation fault anyway). The stack…
Nathan Osman
  • 71,149
  • 71
  • 256
  • 361
22
votes
3 answers

Python - Running Autobahn|Python asyncio websocket server in a separate subprocess or thread

I have a tkinter based GUI program running in Python 3.4.1. I have several threads running in the program to get JSON data from various urls. I am wanting to add some WebSocket functionality to be able to allow program to act as a server and allow…
user2662241
  • 529
  • 2
  • 6
  • 15
22
votes
1 answer

Tkinter IntVar returning PY_VAR0 instead of value

I have a Checkbutton and an IntVar object associated with it, but when I try to get the value of the var, I am receiving PY_VAR0. Here's my code: from tkinter import * root = Tk() def show_state(): print(var) var = IntVar() cbtn =…
Marc43
  • 491
  • 2
  • 6
  • 15
22
votes
2 answers

Is there a way to create transparent windows with Tkinter?

I'm trying, ultimately, to create "oddly-shaped windows" with Python using the Tkinter module. But for now I will settle for being able to make the background transparent while keeping child widgets fully-visible. I'm aware this is done with…
Paul Johnson
  • 321
  • 1
  • 2
  • 4
22
votes
2 answers

How to code the tkinter "scrolledtext" module

The code below produces an ugly but functional example of using a scroll bar in a text widget and results in a couple of questions. Note: this is done using Python 3 on a windows box. The scroll bar that appears is attached to the frame and…
Bill Waters
  • 291
  • 1
  • 4
  • 7
22
votes
3 answers

How to change text cursor color in Tkinter?

I have a text widget with dark background and I can't see the cursor's position. Is there any way to change the (blinking) text cursor's color?
user1967718
  • 835
  • 1
  • 13
  • 22
22
votes
11 answers

Have multiple commands when button is pressed

I want to run multiple functions when I click a button. For example I want my button to look like self.testButton = Button(self, text = "test", command = func1(), command = func2()) when I execute this statement I get an…
user1876508
  • 12,864
  • 21
  • 68
  • 105
22
votes
2 answers

Files from directory being pulled in wrong order with python

I'm populating a tkinter listbox with files from a directory. The names of the files all start with a number from 01 - n. When I view the files in the directory they appear in numerical order. However, when I load the files into a listbox they…
user1104854
  • 2,137
  • 12
  • 51
  • 74
22
votes
8 answers

Truly custom font in Tkinter

I am making an interface in Tkinter and I need to have custom fonts. Not just, say, Helvetica at a certain size or whatever, but fonts other than what would normally be available on any given platform. This would be something that would be kept…
Verdigriss
  • 267
  • 1
  • 2
  • 6