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
2 answers

int() can't convert non-string with explicit base when converting to GUI

I was running a basic hex2dec converter, and wanted to transform this from console to GUI. Now the program works fine in console, but after my conversion to GUI, it seems to throw out the int() can't convert non-string with explicit base error. Here…
David M
  • 53
  • 1
  • 1
  • 4
5
votes
3 answers

Limiting python filedialog to a specific filetype

I created the following trial code to make a program that will load an excel sheet into a dataframe and then save that dataframe to another excel sheet (all of the user's choosing) import os import pandas from pandas import ExcelWriter import…
jon
  • 349
  • 3
  • 4
  • 20
5
votes
1 answer

Changing entry box background colour in tkinter

So I've been working on this program and I'm finding it very hard to figure out what's wrong. I'm fairly new to tkinter so this may be quite minor. I'm trying to get the program to change the entry box's background colour when the check button is…
Bhavik Narang
  • 109
  • 1
  • 2
  • 10
5
votes
2 answers

tkinter thread communication

I have code that should show communication between tkinter widget (NOTE: not implemented yet) and another thread. As communication between those two I choose python queue. To see what is really happening print is shown in console and it's not what I…
flebas
  • 99
  • 8
5
votes
1 answer

Changing Tkinter Label Text Dynamically using Label.configure()

I am new to GUI development and trying to build an application using Python3.6+Tkinter. I have a requirement where I need to change the default text (displayed by the Label when it was created) with a new text when the user hits a Button. To achieve…
MightyInSpirit
  • 145
  • 1
  • 1
  • 9
5
votes
1 answer

tkinter: fill = Y has no effect

from tkinter import * root = Tk() root.title('My app') root.minsize(250, 100) label1 = Label(root, text = 'Hello world!', fg = 'red', bg = 'yellow', font = 'Monaco') label1.pack(fill = X) label2 = Label(root, text = 'Some more…
user5587030
5
votes
3 answers

Sphinx autodoc on readthedocs: ImportError: No module named _tkinter

I'm trying to build documentation on https://readthedocs.org/. I cannot see the docstrings for any files that import matplotlib. When I look into the build log, I see that from matplotlib import pyplot as plt failed, with the…
Peter
  • 12,274
  • 9
  • 71
  • 86
5
votes
1 answer

Tkinter - Columns of equal weight are NOT equal width

I have a Tkinter Toplevel window with three columns. All three columns are configured to have equal weight. Inside column 0 and 2 are sub-frames, inside which are Listbox widgets. Inside column 1 is a set of buttons. For some reason, despite the…
Codesmith
  • 5,779
  • 5
  • 38
  • 50
5
votes
2 answers

Python 3.6 tkinter window icon on Linux error

I am studying Python GUI from Python GUI Programming Cookbook. A certain task requires me to change the window icon by adding the following code to my recipe: # Change the main windows icon win.iconbitmap(r'C:\Python34\DLLs\pyc.ico') Since I am…
5
votes
1 answer

Tkinter Scale slider with float values doesn't work with locale of language that uses comma for floats

I have following code: import Tkinter as tk import locale from Tkinter import * #locale.setlocale(locale.LC_NUMERIC, 'pl_PL') master = tk.Tk() w = tk.Scale(master, from_=0.05, to=0.1, resolution=0.01) w.pack() tk.mainloop() And the slider works…
mys149
  • 97
  • 6
5
votes
0 answers

Is it possible to scroll Treeview's column content, not column window in Tkinter?

I have a window with Treeview widget and a column. If I add enough nodes so that node's text extends beyond window's width, it'll not be possible to scroll horizontally unless I change column width to make it wider than the window width. Here's what…
Doom8890
  • 435
  • 3
  • 15
5
votes
2 answers

tkinter PhotoImage doesn't exist?

from tkinter import * root = Tk() photo = PhotoImage(file='blueface.png') label = Label(root, image=photo) label.pack() root.mainloop() The image face.png is in the same directory as this .py script, but when I run it, I get the following…
Andy G
  • 55
  • 1
  • 7
5
votes
2 answers

Tkinter splash screen & multiprocessing outside of mainloop

I have implemented a splash screen that is shown while my application loads the database from remote cloud storage on startup. The splash screen is kept alive (there's a progressbar on it) with calls to .update() and is destroyed once the separate…
Sam
  • 191
  • 1
  • 7
5
votes
2 answers

Where can I find the source code for the _tkinter module?

According to the Python 3 documentation, Chapter 25.1: tkinter - Python interface to Tcl/Tk: The Tk interface is located in a binary module named _tkinter. This module contains the low-level interface to Tk Where can I obtain the source code of…
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
5
votes
2 answers

What is the purpose of the Frame class in Tkinter?

The code below will build a basic buttons GUI from tkinter import * class App: def __init__(self, master): frame = Frame(master) frame.pack() self.button = Button(frame, text="QUIT", fg="red", …
Belle
  • 451
  • 1
  • 4
  • 9
1 2 3
99
100