Tkinter
Tkinter
|
|
---|---|
A simple user interface with Tkinter |
|
Basic data
|
|
developer | John Ousterhout |
operating system | Platform independent |
programming language | python |
category | GUI toolkit |
License | BSD license |
wiki.python.org/moin/TkInter |
Tkinter is a language connection for the GUI toolkit Tk for the programming language Python . The name is an abbreviation for Tk interface . Tkinter was the first GUI toolkit for Python, which is why it is now included with Python on Mac OS and Windows .
Range of functions
Tkinter makes it possible with Python to create programs with a graphical user interface . These programs and GUIs can be used on Windows, Mac OS and some Linux distributions. Some distributions (Ubuntu, Gentoo) need to install the package first.
There are now several alternatives, including WxPython , PyQt and PySide , PyGTK , Kivy and PyFLTK .
Hello World
A simple hello world program in Python 2.7 with Tkinter:
#Example (Hello, World):
import Tkinter #in python 3.x: tkinter wird kleingeschrieben
tk = Tkinter.Tk()
frame = Tkinter.Frame(tk, relief="ridge", borderwidth=2)
frame.pack(fill="both",expand=1)
label = Tkinter.Label(frame, text="Hallo Welt!")
label.pack(expand=1)
button = Tkinter.Button(frame,text="OK",command=tk.destroy)
button.pack(side="bottom")
tk.mainloop()
Controls and layout manager
Tkinter makes it possible to integrate various control elements in the window. These include B. buttons (buttons), slider and labels. These can be integrated into the window by three different methods using the layout manager. On the one hand with the .pack () command, the .grid () command and on the other hand with the .place () command.
Grid manager
The grid manager enables control elements to be integrated into the window in a kind of table, i.e. it is essentially based on a table that is arranged in rows and columns. 'Row' and 'column' are specified for the arrangement, i. H. Row and column.
Example of an arrangement using the grid manager:
from Tkinter import *
fenster = Tk()
fenster.title("Wikipedia")
fenster.geometry("200x50")
label = Label(fenster, text = "Hallo Welt!")
label.grid(row = 1, column = 1) #Anordnung durch Grid-Manager
fenster.mainloop()
Pack manager
The pack manager is (for the developer) the simplest layout manager that Python comes with. Instead of giving the program precise information about where you want to place the respective control element, you only have to use the .pack () command. Python then arranges the element by itself and integrates it into the program.
Example of an arrangement using the Pack Manager:
from Tkinter import *
fenster = Tk()
fenster.title("Wikipedia")
fenster.geometry("200x50")
label = Label(fenster, text = "Hallo Welt!")
label.pack() #Anordnung durch Pack-Manager
fenster.mainloop()
Place manager
The Place Manager, on the other hand, allows the position to be specified very precisely. Information about the x- and y-position of the control elements is made. It should be noted here that the positive y-direction goes down.
Example of an arrangement using the place manager:
from Tkinter import *
fenster = Tk()
fenster.title("Wikipedia")
fenster.geometry("200x50")
label = Label(fenster, text = "Hallo Welt!")
label.place(x = 10, y = 10) #Anordnung durch Place-Manager
fenster.mainloop()
SimpleTk
SimpleTk is an external module that makes it easier to create graphical interfaces with Tkinter. A separate syntax is used for this , which, for example, shows dependencies of objects ( widgets ) with indentations (tabs). The design of the GUI is exported to an external file and imported into the Python script. The code in the actual program becomes clearer and smaller tests can be implemented more easily.
Implementation in the Python script:
from tkinter import *
from SimpleTk import SimpleTk
root = Tk()
stk = SimpleTk(root)
root.mainloop()
Example for the SimpleTk syntax:
Style: Label(bg = "lightblue")
Style: LabelFrame(bg = "gold")
Frame: frame1 (bg = "white") {1}
LabelFrame: lf1 (text = "Header") {2}
Label: label1 (text = "My..first..GUI")
Button: button1 (text = "Thi..i..a..Button")
Entry: entry1 (bg = "white")
LabelFrame: lf2 (text = "Footer")
Label: label2 (text = "Footer")
ttk.Combobox: cb1 ()
> cb1.set("Selection")
Equivalent to traditional Tkinter syntax:
frame1 = Frame(root, bg = "white")
frame1.pack(padx = 5, pady = 5, expand = True, fill = BOTH)
lf1 = LabelFrame(frame1, text = "Header", bg = "gold")
lf1.grid(row = 0, column = 0, padx = 5, pady = 5, sticky = N+S+E+W)
label1 = Label(lf1, text = "My first GUI", bg = "lightblue")
label1.grid(row = 0, column = 0, padx = 5, pady = 5, sticky = N+S+E+W)
button1 = Button(lf1, text = "This is a Button")
button1.grid(row = 0, column = 1, padx = 5, pady = 5, sticky = N+S+E+W)
entry1 = Entry(lf1, bg = "white")
entry1.grid(row = 1, column = 0, padx = 5, pady = 5, sticky = N+S+E+W)
lf2 = LabelFrame(frame1, text = "Footer", bg = "gold")
lf2.grid(row = 1, column = 0, padx = 5, pady = 5, sticky = N+S+E+W)
label2 = Label(lf2, text = "Footer", bg = "lightblue")
label2.pack(padx = 5, pady = 5, expand = True, fill = BOTH)
cb1 = ttk.Combobox(lf2)
cb1.pack(padx = 5, pady = 5, expand = True, fill = BOTH)
cb1.set("Selection")
The widgets can also be called afterwards as attributes of the SimpleTk object in the Python script. This enables the specific widget and geometry attributes to be changed later.
Web links
- Tkinter website (English)
- Introduction to Tkinter (English)
- GUIbuilder for tkinter
- SimpleTk documentation
Individual evidence
- ↑ GUI programming with Python: layout manager and layout management in Tkinter. Retrieved February 20, 2017 .
- ↑ simpleTk: A package to create simple Tkinter-GUIs using a separate text file. Retrieved August 21, 2019 .