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

How do i resize the pop-up from simpledialog.askstring in tkinter?

While using the simplesialog.askstring is good and all, I would like to resize the pop-up window and also resize the width of the text input. (sample code) from tkinter import * from tkinter import simpledialog prompts = ["name", "age", "height",…
5
votes
2 answers

tkinter scrollable canvas after adding widgets with grid manager

I'm trying to create a canvas widget with a number of widgets embedded within it. Since there will frequently be too many widgets to fit in the vertical space I have for the canvas, it'll need to be scrollable. import tkinter as tk #…
Carl Kevinson
  • 794
  • 4
  • 16
5
votes
1 answer

Hide a Button in Tkinter

I want to hide a tkinter button but not when the user clicks it. I just want to hide it, at random times. How will I do that in Python? Below is the code I tried: self.startGame = Button(self.canvas, text="Start", background='white', command =…
alyssaeliyah
  • 2,214
  • 6
  • 33
  • 80
5
votes
1 answer

Tkinter - Maximizing and minimizing the window before using any widgets, hides the widgets when I use them

This is the code that I have written. As you can see, it has a button which when clicked opens up a Text where one can enter something. It works, the window resizes if you click the button. But I have noticed that, if I maximize and minimize the…
Miraj50
  • 4,257
  • 1
  • 21
  • 34
5
votes
2 answers

Correct way to set scrollbar position in python tkinter

I am making a basic text editor and I am saving the scroll position in a file on closing the program. Then when opening the program it will read the scroll position from the file and update it so you can continue where you left off. I can get the…
JohnT
  • 151
  • 1
  • 16
5
votes
2 answers

Pyinstaller generated exe doesn't work properly

I'm trying to package a python program/script (pastebin link) I wrote which includes a GUI using the tkinter module. I decided to use Pyinstaller and according to them it supports Python 3.7. Currently, trying to run pyinstaller seems to generate no…
Thunderpurtz
  • 189
  • 2
  • 12
5
votes
0 answers

Why can you adjust font size for messagebox in Python 2 but not in Python 3?

I was looking to add a simple message box to a program and it looks like you can't adjust the font size in python 3. That's annoying but not too bad - I'll just make my own window and get over it. What I don't understand is why the font was…
Jack Duane
  • 185
  • 3
  • 17
5
votes
1 answer

ttk.Spinbox missing in tkinter.ttk?

The tkinter version I am using is accessing tk.TclVersion=8.6. I am able to access stylename='TSpinbox' from ttk.Style(). Stylename = TSpinbox Layout = [('Spinbox.field', {'side': 'top', 'sticky': 'we', 'children': [('null', {'side': 'right',…
Sun Bear
  • 7,594
  • 11
  • 56
  • 102
5
votes
4 answers

Cannot import Tkinter on visual studio code?

Ok, I am having a weird issue going on. From what I understand Tkinter is supposed to be built in with python 2 and 3. I can import and use Tkinter just fine in my terminal under python3 as well as with IDLE3. However, when I try to import Tkinter…
Robotica
  • 75
  • 1
  • 1
  • 8
5
votes
1 answer

PYTHON 3.7 _tkinter.TclError: invalid command name "tixBalloon"

Hello I am trying to create a button that will show description when hovered. similar to html img tag "alt" I decide to use "tkinter.pix" with Balloon() But I am having an error: _tkinter.TclError: invalid command name "tixBalloon". from tkinter…
Evan
  • 2,327
  • 5
  • 31
  • 63
5
votes
1 answer

PyInstaller App not opening on Mac

I'm trying to create an app for a program I have written. I have no problems doing it on Windows, but I've been trying for a week to get an OSX version working. Here is what my terminal looks like: Thomass-iMac:gene_expression Tomallama$ python3…
Thomas Kellough
  • 143
  • 1
  • 2
  • 11
5
votes
3 answers

Tkinter: How to make a rounded corner text widget?

Question How to create a rounded corner text widget? I think I have an idea of creating a rounded canvas and fill the entire canvas with the text box with no border. Problem The code to create a rounded border canvas is not working when I try to…
Raj Mehta
  • 314
  • 1
  • 4
  • 20
5
votes
2 answers

Cannot change tkinter font on pixelbook

I am using python3 with tkinter on my pixelbook chromebook using native Linux in developer branch. It's an Anaconda distribution (python 3.6.5), and all packages work as expected, except this small kink in tkinter. The default font size in tkinter…
Andrei K.
  • 191
  • 1
  • 7
5
votes
1 answer

How to structure complex object oriented tkinter program

I have written multiple gui applications using python, but each one is a mess of initializations and functions and I don't feel like I'm using the object oriented capabilities to their full potential. I have searched the net, but haven't found any…
saladboy97
  • 51
  • 3
5
votes
2 answers

Ask a user to select folder to read the files in Python?

I have a script that I need to ask the user to select the folder where reside two subfolders that contain each one file that are needed to be read. Kind of giving their directory. I was thinking about a pop up where they choose the folder. There is…
user122244
  • 109
  • 1
  • 2
  • 10