Python graphical user interface and python graphical interface
Tkinter is one of the many tool sets in Python that can be used to build a GUI.
Tkinter Module
# You can use import tkinter as tk and use tk. thing to reference the content from tkinter import * window = Tk () window. mainloop ()
The preceding Code displays a blank root window. It can be seen as the outermost container of the application. It is used when other plug-ins (widgets) are created. If the window on the screen is closed, the corresponding window object will be destroyed. All applications have only one main window. In addition, you can use the TopLevel plug-in to create additional windows.
Tkinter plug-inIncluding Button, Canvas, Checkbutton, Entry, Frame, Label, Listbox, Menu, Message, Menubutton, Text, TopLevel, etc.
Variable
In Python, strings, integers, floating-point numbers, and boolean values are immutable, so tkinter comes with some types; they can be updated locally, you can also notify related plug-ins when their values change.
Variable type in tkinter
| Unchangeable type |
Variable type |
| Int |
IntVar |
| String |
StringVar |
| Bool |
BooleanVar |
| Double |
DoubleVar |
Model, view, Controller
As the name suggests, a view is used to display information to users. A model only stores data. A controller can update the model of an application and change the view accordingly.
The following example shows how to increase the count on the tag after clicking the button:
from tkinter import *# The controller.def click(): counter.set(counter.get() + 1)if __name__ == '__main__': # More initialization window = Tk() # The model. counter = IntVar() counter.set(0) # The views. frame = Frame(window) frame.pack() button = Button(frame, text='Click', command=click) button.pack() label = Label(frame, textvariable=counter) label.pack() window.mainloop()
Use Lambda
If we want to increase the counter value, we also want to decrease it. Then we need to add another button and another controller function. The Code is as follows:
from tkinter import *# The controller.def click_up(): counter.set(counter.get() + 1)def click_down(): counter.set(counter.get() - 1)if __name__ == '__main__': # More initialization window = Tk() # The model. counter = IntVar() counter.set(0) # The views. frame = Frame(window) frame.pack() button = Button(frame, text='Up', command=click_up) button.pack() button = Button(frame, text='Down', command=click_down) button.pack() label = Label(frame, textvariable=counter) label.pack() window.mainloop()
The above implementation code looks silly,click_upAndclick_downThe things seem almost the same and should be merged into one. In this case, the counter should be passed to the function rather than the global variable.
# The model.counter = IntVar()counter.set(0)# One controller with parametersdef click(variable, value): varaible.set(variable.get() + value)
Tkinter requires that the controller functions departing from buttons (and other plug-ins) do not contain parameters, so that they can be called in the same way. What we need to do is to process the function with two parameters so that it becomes a function without parameters.
A good practice is to use the lambda function, which enables us to create a single-row function without a name.
from tkinter import *window = Tk()# The modelcounter = IntVar()counter.set(0)# General controller.def click(var, value): var.set(var.get() + value)# The views.frame = Frame(window)frame.pack()button = Button(frame, text='Up', command=lambda: click(counter, 1))button.pack()button = Button(frame, text='Down', command=lambda: click(counter, -1))button.pack()label = Label(frame, textvariable=counter)label.pack()window.mainloop()
This Code creates a lambda function without parameters for the two buttons respectively. These two lambda functions will pass the correct values to the click function.
Style
From tkinter import * window = Tk () # font button = Button (window, text = 'hello', font = ('courier ', 14, 'bold italic ')) # layout button. pack (side = 'left') # color label = Label (window, text = 'hello', bg = 'green', fg = 'white') label. pack () window. mainloop ()
To control the layout, you can use the pack or grid, but not both.
from tkinter import *window = Tk()button = Button(window, text='button1', font=('Courier', 14, 'bold italic'))button.grid(row=0, column=0)label = Label(window, text='label1', bg='green', fg='white')label.grid(row=0, column=1)label = Label(window, text='label2', bg='green', fg='white')label.grid(row=1, column=1)window.mainloop()
You can use rowspan and columnspan to set the number of lines occupied by the plug-in. The default value is 1.
Object-oriented GUI
Almost all real guis are built with classes and objects: they share models, views, and controllers in a clean package. For example, the following Counter function is a Counter class namedself.stateThe member variable whose controller isupClickAndquitClickMethod.
from tkinter import *class Counter: '''A simple counter GUI using object-oriented programming.''' def __init__(self, parent): '''Create the GUI.''' # Framework. self.parent = parent self.frame = Frame(parent) self.frame.pack() # Model. self.state = IntVar() self.state.set(1) # Label displaying current state. self.label = Label(self.frame, textvariable=self.state) self.label.pack() # Buttons to control application. self.up = Button(self.frame, text='up', command=self.upClick) self.up.pack(side='left') self.right = Button(self.frame, text='quit', command=self.quitClick) self.right.pack(side='left') def upClick(self): '''Handle click on 'up' button.''' self.state.set(self.state.get() + 1) def quitClick(self): '''Handle click on 'quit' button.''' self.parent.destroy()if __name__ == '__main__': window = Tk() myapp = Counter(window) window.mainloop()
References:
Python programming practices
Practical Programming An Introduction to Computer Science Using Python