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

How to get the selected date for DateEntry in tkcalendar (Python)?

I have a tkcalendar and it's pre-defined widget for Calendar, DateEntry and am trying to get the User's selected date for DateEntry. Whereas there is provision to extract the selected date for the Calendar widget using "selection_get()" but nothing…
Garima Tiwari
  • 1,490
  • 6
  • 28
  • 46
5
votes
1 answer

Creating a grid lines inside of tkinter treeview

I have one simple treeview made in tkinter. Is it possible to make a grid inside the tkinter treview, so that it looks more like a table? I want to make it more "user-friendly", so visualization of the table/treeview can be better. from tkinter…
Aleksandar Beat
  • 191
  • 7
  • 22
5
votes
1 answer

RTL (right-to-left) language inTKinter

I'm using Tkinter for my Gui (Python). Until now the program has supported only LTR language. Now I need to also support RTL language (Hebrew). I saw answers that handle with mirror text, but that's not my problem. I'm having an issue while using…
rinat dior
  • 49
  • 4
5
votes
2 answers

Python | tkinter: What does tkinter.END do?

Learning python through a book, and tkinter.END is used in a block of code without being explained import tkinter def count(text, out_data): """ Update out_data with the total number of As, Ts, Cs, and Gs found in text.""" data =…
Yabusa
  • 579
  • 1
  • 6
  • 15
5
votes
1 answer

Can't change button font size in tkinter

I can't seem to change the size of my font in tkinter! No matter which size I choose, the button text displays the same. If I deleted the whole stlye line, it's displayed smaller. Similarly, the font always looks the same, no matter what I…
user7088941
  • 261
  • 1
  • 4
  • 11
5
votes
3 answers

How do i clear a window after pressing a button in Python Tkinter?

I am currently creating a math's quiz for kids in python tkinter. In this quiz i have 3 different 'pages' as per say. A start page, a quiz page and a score page for when the quiz is finished. In my start page, i have three different difficulties of…
Joe Stanford
  • 51
  • 1
  • 4
5
votes
2 answers

How to set the transparency of the tkinter frame?

Python version 2.7 Code: from Tkinter import * root = Tk(); root.geometry ('{}x{}'.format(w,h)); left_frame = Frame(root, width = w*0.8, height=400, bg='#988C89'); right_frame = Frame(root, bg='#988C89', width = w*0.8, height=400…
김창회
  • 53
  • 1
  • 1
  • 4
5
votes
4 answers

set window icon tkinter macosx

This line works fine for my Windows program. When i run this same file on the Mac OS X, I get a blank page instead of my icon. Here is the windows line: self.iconbitmap("Boss.ico") I have searched relentlusly for an answer I want this icon to work…
Garry Hurst
  • 93
  • 11
5
votes
4 answers

Tkinter, transparent background, Linux

Is there a way to get a transparent background in a Tkinter window on Linux? The only way I see currently is: import tkinter as tk root = tk.Tk() root.overrideredirect(True) root.wait_visibility(True) root.wm_attributes("-alpha", 0.0) canvas =…
Drew
  • 1,171
  • 4
  • 21
  • 36
5
votes
2 answers

tkinter - define "custom colors" for colorchooser

I am currently using a colorchooser from tkinter in a project to allow the user to choose a custom colour. Minimally, this can be created (in Python 3.x) through from tkinter import colorchooser cp = colorchooser.askcolor() When this window…
asongtoruin
  • 9,794
  • 3
  • 36
  • 47
5
votes
1 answer

Tkinter does not update while withdrawn

I have a program that will show an image on screen when a hotkey is pressed on my keyboard. Depending on which key was pressed a different image will be shown. After 3 seconds of no input my root Tk gets withdraw()n so there is nothing on screen.…
Aviss
  • 63
  • 5
5
votes
1 answer

Using PIL's ImageDraw Module

I'm trying to do individual pixel manipulation using PIL's ImageDraw Module. The code bellow is supposed to create Tkinter canvas widget. Then open an image, change one pixel's color to red, then embed the image in the canvas widget. However, it…
5
votes
2 answers

How can I set the row height in Tkinter TreeView?

I wrote a small app recently that needs to be cross-platform. I used Python and Tkinter for the GUI. It works great but recently I got a new laptop with a hiDPI screen and it seems to mess up the TreeView (see image below). The text height is too…
Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
5
votes
3 answers

Import _tkinter or tkinter?

All tutorials simply import tkinter, I am wondering, though, why not import _tkinter? If my understanding is correct, _tkinter is the actual library in cpython and tkinter is the interface or API. I am simply trying to grasp the paradigm as I read…
IAbstract
  • 19,551
  • 15
  • 98
  • 146
5
votes
0 answers

Why does tcl/tkinter only support BMP characters?

I am trying to query and display utf-8 encoded characters in a gui built on tkinter and thus tcl. However, I have found that tkinter cannot display 4-byte characters i.e. unicode codepoints greater than U+FFFF. Why is this the case? What limitations…
Alec White
  • 172
  • 1
  • 9