| Tkinter | |
|---|---|
The IDLE Python editor | |
| License | Python license |
| Website | https://docs.python.org/3/library/tkinter.html |
Tkinter is a binding to the Tk GUI toolkit for Python. It is the standard Python interface to the Tk GUI toolkit, and is Python's de facto standard GUI. Tkinter is included with standard Linux, Microsoft Windows and macOS installs of Python.
The name Tkinter comes from Tk interface. Tkinter was written by Steen Lumholt and Guido van Rossum, then later revised by Fredrik Lundh.
Tkinter is free software released under a Python license.
As with most other modern Tk bindings, Tkinter is implemented as a Python wrapper around a complete Tcl interpreter embedded in the Python interpreter. Tkinter calls are translated into Tcl commands, which are fed to this embedded interpreter, thus making it possible to mix Python and Tcl in a single application. The official Python release uses Tcl/Tk 8.6 (rather than the more up-to-date 9.0).
There are several popular GUI library alternatives available, such as Kivy, Pygame, Pyglet, PyGObject, PyQt, PySide, and wxPython.
from tkinter import *
def calculate():
price = float(entry_price.get())
qty = float(entry_qty.get())
total = price * qty
label_result.config(text="Total: " + str(total))
app = Tk()
app.title("Facturation App")
Label(app, text="Prix").pack()
entry_price = Entry(app)
entry_price.pack()
Label(app, text="Quantité").pack()
entry_qty = Entry(app)
entry_qty.pack()
Button(app, text="Calculer", command=calculate).pack()
label_result = Label(app, text="")
label_result.pack()
app.mainloop()
The generic term for any of the building blocks that make up an application in a graphical user interface.
Tkinter provides three modules that allow pop-up dialogs to be displayed: tk.messagebox (confirmation, information, warning and error dialogs), tk.filedialog (single file, multiple file and directory selection dialogs) and tk.colorchooser (colour picker).
Python 2.7 and Python 3.1 incorporate the "themed Tk" ("ttk") functionality of Tk 8.5. This allows Tk widgets to be easily themed to look like the native desktop environment in which the application is running, thereby addressing a long-standing criticism of Tk (and hence of Tkinter). Some widgets are exclusive to ttk, such as the combobox, progressbar, treeview, notebook, separator and sizegrip.
In Tkinter, the Frame widget is the basic unit of organization for complex layouts. A frame is a rectangular area that can contain other widgets.
When any widget is created, a parent–child relationship is created. For example, if you place a text label inside a frame, the frame is the parent of the label.
Below is a minimal Python 3 Tkinter application with one widget:
#!/usr/bin/env python3
from tkinter import *
root = Tk() # Create the root (base) window
w = Label(root, text="Hello, world!") # Create a label with words
w.pack() # Put the label into the window
root.mainloop() # Start the event loop
For Python 2, the only difference is the word "tkinter" in the import command will be capitalized to "Tkinter".
There are four stages to creating a widget
These are often compressed, and the order can vary.
Using the object-oriented paradigm in Python, a simple program would be (requires Tcl version 8.6, which is not used by Python on MacOS by default):
#!/usr/bin/env python3
import tkinter as tk
class Application(tk.Frame):
"""Application holds state for the whole app."""
def __init__(self, root=None):
tk.Frame.__init__(self, root)
self.grid()
self.createWidgets()
def createWidgets(self):
self.medialLabel = tk.Label(self, text="Hello World")
self.medialLabel.config(bg="#00ffff")
self.medialLabel.grid()
self.quitButton = tk.Button(self, text="Quit", command=self.quit)
self.quitButton.grid()
app = Application()
app.root = tk.Tk()
app.root.title("Sample application")
app.mainloop()