Day 27 - 使用 Tkinter 构建图形界面 (GUI) 与高级函数参数 - Python学习笔记
Udemy - 100 Days of Code: The Complete Python Pro Bootcamp
Day 27 - Intermediate - GUI with Tkinter & Advanced Function Arguments
目录
- 205. Window and Label
- 206. Default Arguments
- 207. *args: Many Positional Arguments
- 208. **kwargs: Many Keyword Arguments
- 209. Button and Entry
- 210. Other Tkinter Widgets
- 211. Tkinter Layout Managers: pack, grid, place
- 212. Mile to Kilometers Converter Project
205. Window and Label
Tkinter Documentation
Tk Commands
import tkinter as tk# Create a new window
window = tk.Tk()
window.title("My First GUI Program")
window.minsize(width=500, height=500) # Label
label = tk.Label(text="Old Text", font=("Arial",24,"bold"))
label.pack() # Change label properties
label["text"] = "New Text"
label.config(text="New Text")window.mainloop()
206. Default Arguments
# Default Arguments 默认参数
def my_function(a=1, b=2, c=3):print(a, b, c) my_function(b=5)
207. *args: Many Positional Arguments
# *args 可变位置参数
def add(*args):print(args) # collect positional arguments into a tupleresult = 0 for n in args:result += nprint(result)add(3, 5, 6, 2, 1, 7, 4, 3)
208. **kwargs: Many Keyword Arguments
# **kwargs 可变关键字参数
def calculate(n, **kwargs): print(kwargs) # collect keyword arguments into a dictionaryn += kwargs.get("add", 0) n -= kwargs.get("subtract", 0) n *= kwargs.get("multiply", 1) n /= kwargs.get("divide", 1) print(n) calculate(2, add=3, multiply=5)# Create a class with **kwargs
class Car: def __init__(self, **kw): self.make = kw.get("make") self.model = kw.get("model") self.colour = kw.get("colour") self.seats = kw.get("seats") my_car = Car(make="Nissan", model="Skyline")
print(my_car.model)
209. Button and Entry
# Button
def button_used(): print("I got clicked")
button = tk.Button(text="Click Me", command=button_used)
button.pack()# Entry
entry = tk.Entry(width=30)
entry.focus() # Put cursor in entry
entry.insert(tk.END, "Some text to begin with.")
entry.pack()
print(entry.get()) # Get text in entry
210. Other Tkinter Widgets
# Text
text = tk.Text(height=5, width=30)
text.focus() # Put cursor in textbox
text.insert(tk.END, "Some text to begin with.")
text.pack()
print(text.get("1.0", tk.END)) # Get text from line 1, char 0 to the end# Spinbox
def spinbox_used(): print(spinbox.get())
spinbox = tk.Spinbox(from_=0, to=10, width=5, command=spinbox_used)
spinbox.pack() # Scale
def scale_used(value): print(value)
scale = tk.Scale(from_=0, to=100, command=scale_used)
scale.pack() # Checkbutton
def cb_used(): print(cb_state.get())
cb_state = tk.IntVar() # Variable to hold the checkbutton state
cb = tk.Checkbutton(text="Is On?", variable=cb_state, command=cb_used)
cb.pack() # Radiobutton
def rb_used(): print(rb_state.get())
rb_state = tk.IntVar() # Variable to hold the selected radiobutton value
rb1 = tk.Radiobutton(text="Option1", value=1, variable=rb_state, command=rb_used)
rb2 = tk.Radiobutton(text="Option2", value=2, variable=rb_state, command=rb_used)
rb1.pack()
rb2.pack()# Listbox
def listbox_used(event): print(listbox.get(listbox.curselection()))
listbox = tk.Listbox(height=4)
fruits = ["Apple", "Pear", "Orange", "Banana"]
for item in fruits: listbox.insert(fruits.index(item), item)
listbox.bind("<<ListboxSelect>>", listbox_used)
listbox.pack()
211. Tkinter Layout Managers: pack, grid, place
label.pack(side="left") # 纵向/横向堆叠
label.grid(column=0, row=0) # 表格
label.place(x=0, y=0) # 绝对坐标
212. Mile to Kilometers Converter Project
from tkinter import * def mile_to_km(): miles = float(miles_input.get()) km = round(miles * 1.609, 2) km_result_label.config(text=f"{km}") window = Tk()
window.title("Miles to Km Converter")
window.config(padx=20, pady=20) miles_input = Entry(width=7)
miles_input.focus()
miles_input.grid(column=1, row=0) miles_label = Label(text="Miles")
miles_label.grid(column=2, row=0) is_equal_label = Label(text="is equal to")
is_equal_label.grid(column=0, row=1) km_result_label = Label(text="0")
km_result_label.grid(column=1, row=1) km_label = Label(text="Km")
km_label.grid(column=2, row=1) calculate_button = Button(text="Calculate", command=mile_to_km)
calculate_button.grid(column=1, row=2) window.mainloop()