https://docs.python.org/3/library/tkinter.html
https://realpython.com/python-gui-tkinter/
https://tkdocs.com/
https://www.tutorialspoint.com/python/python_gui_programming.htm
Python - GUI Programming (Tkinter)
https://riptutorial.com/tkinter
Tkinter comes with the Python installer so we don't need to install it separately. But it is possible that Tkinter was accidentally excluded from the list of Python features, especially on Windows.
CONDA # Make sure the conda package tk is installed: conda list tk # If it is not installed, run: conda install tk PIP (first check that Tkinter is really missing) pip install tk APT (for the entire computer) Debian packages: python-tk, python3-tk, and dependencies.
The 'tkinter' package is the standard Python interface to the Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems. Tk itself is not part of Python; it is maintained at ActiveState.
# This should open a window demonstrating a simple Tk interface. $ python -m Tkinter # testing in Py2, version 8.6 in Debian 10 $ python3 -m tkinter # testing in Py3, version 8.6 in Debian 10
# To check the Tkinter version, type the following commands in your Python REPL: #import Tkinter as tk # Py2 import tkinter as tk # Py3 or Py2 in Debian tk._test() # this should open a window
Tkinter is lightweight and cross-platform, so the same code works on Windows, MacOS, and Linux.
Although Tkinter is considered the de-facto standard Python GUI framework, it’s not without criticism. One notable criticism is that GUIs built with Tkinter look outdated.
Tkinter is largely unchanged between Python 2 and Python 3, with the major difference being that the tkinter package and modules were renamed.
# Importing in Py2.
import Tkinter as tk
import tkFileDialog as filedialog
root = tk.Tk() # this is the root widget
#root.configure(width=200, height=100) # optional
#root.geometry("200x100") # optional, the same
#root.geometry("{}x{}".format(width, height))
#root.geometry("{}x{}+{}+{}".format(width, height, pos_x, pos_y))
#root.resizable(width=False, height=False) # the window size is fixed
root.title("first") # optional, the default title is "tk"
# Widgets are added here.
label = tk.Label(root, text="Hello", bg="red") # a label widget
label.grid() # using a geometry manager
#label.grid(row=0, column=0) # the position on the grid, top left corner
root.mainloop() # run the tkinter event loop
# Importing in Py3. import tkinter as tk from tkinter import filedialog root = tk.Tk() # this is the root widget # Widgets are added here. root.mainloop() # run the tkinter event loop
# In simple scripts we can use a universal form of importing.
try:
import Tkinter as tk # Py2
import tkFileDialog as filedialog
import tkMessageBox as messagebox
import ttk # themed widgets
except ImportError:
import tkinter as tk # Py3
from tkinter import filedialog
from tkinter import messagebox
from tkinter import ttk # themed widgets
The statement 'root.mainloop()' tells Python to run the Tkinter 'event loop'. This method listens for events, such as button clicks or keypresses, and blocks any code that comes after it from running until the window it’s called on is closed.