Tkinter is one of the many toolsets available in Python to build a GUI.
Tkinter Module
# 可以使用import tkinter as tk并通过tk.thing去引用其中的内容import *window = Tk()window.mainloop()
The above code can display a blank root window. It can be thought of as the outermost container of an application, and it needs to be used when creating other plug-ins (widgets). If you close the window on the screen, the corresponding Window object is destroyed. All applications have only one main window, and additional windows can be created by toplevel this widget.
tkinter widget includes button, Canvas, Checkbutton, Entry, Frame, Label, Listbox, Menu, Message, Menubutton, Text, toplevel such as
Variable variable
strings, integers, floating-point numbers, and Boolean values are immutable in Python, so tkinter comes with some types, they can be updated in place, and the relevant plug-ins can be notified when their values change.
mutable types in the Tkinter
Immutable Types |
Variable Type |
Int |
Intvar |
String |
Stringvar |
bool |
Booleanvar |
Double |
Doublevar |
Models, views, controllers
As the name implies, the view is used to display information to the user; The model simply stores the data, and the controller can update the application's model and proceed with the corresponding view changes.
The following example realizes that the count on the label increases after the click 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=c Lick) button.pack () label = label (frame, Textvariable=counter) Label.pack () Window.mainloop ()
Using lambda
If we want not only to increase the value of counter, but also to reduce its value. Then we need to add another button and another controller function. The code is as follows:
fromTkinterImport*# the Controller. def click_up():Counter.set (Counter.get () +1) def click_down():Counter.set (Counter.get ()-1)if__name__ = =' __main__ ':# More Initializationwindow = 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 implementation code above looks a little silly, click_up
and click_down
the things you do look almost the same, and you should synthesize them one. At this point, we should show the counter passed to the function instead of using global variables.
# The model.counter = IntVar()counter.set(0)# One controller with parametersdef click(variable, value): varaible.set(variable.get() + value)
Tkinter the controller functions that are required by buttons (and other plug-ins) cannot contain parameters to invoke them in the same way. The thing we're going to do is to deal with this two-parameter function so that it becomes a function without parameters.
A good thing to do is to use a lambda function, which allows us to create a single-line function without a name.
fromTkinterImport*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 with no parameters for two buttons, and these two lambda functions pass the correct values into the click.
Style
import *window = Tk()# 字体button = Button(window, text=‘hello‘, font=(‘Courier‘14‘bold italic‘))# 布局button.pack(side=‘left‘)# 颜色label = Label(window, text=‘hello‘, bg=‘green‘, fg=‘white‘)label.pack()window.mainloop()
Control the layout, you can use the pack, or you can use the grid, but not both.
From Tkinter Import *window= Tk ()Button= Button (window,text=' button1 ', Font= (' Courier ', -,' 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 rows occupied by the plug-in, which defaults to 1.
Object-oriented GUI
Almost all real GUIs are built with classes and objects: They talk about models, views, and controllers in a neat package. For example, the following counter function whose model is a member variable named for the Counter class self.state
, its controller is the upClick
and quitClick
method.
fromTkinterImport* 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 the ' 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 ()
Resources:
"Python Programming Practice"
"Practical programming an Introduction-computer science Using Python"
Python Graphical user interface