Python Chapter 9: 使用Tkinter進行GUI程序設計 Part 2
6.幾何管理器
Tkinter中的幾何管理器分為:網格管理器、包管理器與位置管理器(最好不要對同一容器中的小構件使用多種管理器)
①網格管理器:將各個空間分布在看不見的網格中
1 # Program 9.7 2 from tkinter import * 3 4 class GridManagerDemo: 5 window = Tk() 6 window.title("Grid Manager Demo") 7 8 message = Message(window, text = "This Message widget occupies three rows and two columns") 9 message.grid(row = 1, column = 1, rowspan = 3, columnspan = 2) 10 Label(window, text = "First Name:").grid(row = 1, column = 3) 11 Entry(window).grid(row = 1, column = 4, padx = 5, pady = 5) 12 Label(window, text = "Last Name:").grid(row = 2, column = 3) 13 Entry(window).grid(row = 2, column = 4)14 Button(window, text = "Get Name").grid(row = 3, column = 4, padx = 5, pady = 5, sticky = E) 15 16 window.mainloop() 17 18 GridManagerDemo()
9) rowspan與columnspan將message放置在多行多列中
11、14) padx與pady填充單元格在水平與豎直位置上的可選空間,sticky = E使Button設置在單元格東邊,E亦可以是W、S、N、NW、NE等組合
② 包管理器:將小構件一次一個放在另一個頂部或者一個挨著一個放置
1 # Program 9.8 2 from tkinter import * 3 4 class PackManagerDemo: 5 def __init__(self): 6 window = Tk() 7 window.title("Pack Manager Demo 1") 8 9 Label(window, text = "Blue", bg = "blue").pack() 10 Label(window, text = "Red", bg = "red").pack(fill = BOTH, expand = 1) 11 Label(window, text = "Green", bg = "green").pack(fill = BOTH) 12 13 window.mainloop() 14 15 PackManagerDemo()
10)fill參數選擇X,Y或BOTH,代表小控件填充水平、豎直或兩個方向上的空間
expand參數非零時,若父小構件比容納所有打包小構件的所需空間都大,則將額外的空間分配給小構件
其運行結果如下(從上到下依次排布,有fill的會填充橫縱向,有expand會被分配多余的空間):
1 # Program 9.9 2 from tkinter import * 3 4 class PackManagerDemoWithSide: 5 window = Tk() 6 window.title("Pack Manager Demo 2") 7 8 Label(window, text = "Blue", bg = "blue").pack(side = LEFT) 9 Label(window, text = "Red", bg = "red").pack(side = LEFT, fill = BOTH, expand = 1) 10 Label(window, text = "Green", bg = "green").pack(side = LEFT, fill = BOTH) 11 12 window.mainloop() 13 14 PackManagerDemoWithSide()
8-10) side = LEFT意味著靠左放,該選項可以是LEFT/RIGHT/TOP/BOTTOM
其運行結果如下(從左到右依次排布,有fill的會填充橫縱向,有expand會被分配多余的空間):
③ 位置管理器:將小構件放在絕對位置上
1 # Program 9.10 2 from tkinter import * 3 4 class PlaceManagerDemo: 5 def __init__(self): 6 window = Tk() 7 window.title("Place Manager Demo") 8 9 Label(window, text = "Blue", bg = "blue").place(x = 20, y = 20) 10 Label(window, text = "Red", bg = "red").place(x = 50, y = 50) 11 Label(window, text = "Green", bg = "green").place(x = 80, y = 80) 12 13 window.mainloop() 14 15 PlaceManagerDemo()
9-11) 小控件.place(x, y)用位置管理器將小控件放在一定的絕對位置上
其運行結果如下:
註意:位置管理器不能兼容所有計算機,受分辨率的影響而影響,應盡量避免使用位置管理器
7. 實例研究:貸款計算器
開發一個GUI程序的主要步驟:
1)繪制輪廓圖來設計用戶界面
2)處理按鈕等引發的事件
1 # Program 9.11 2 from tkinter import * 3 4 class LoanCalculator: 5 def __init__(self): 6 window = Tk() 7 window.title("Loan Calculator") 8 9 Label(window, text = "Annual Interest Rate").grid(row = 1, column = 1, sticky = W) 10 Label(window, text = "Number of Years").grid(row = 2, column = 1, sticky = W) 11 Label(window, text = "Loan Amount").grid(row = 3, column = 1, sticky = W) 12 Label(window, text = "Monthly Payment").grid(row = 4, column = 1, sticky = W) 13 Label(window, text = "Total Payment").grid(row = 5, column = 1, sticky = W) 14 15 self.annualInterestRateVar = StringVar() 16 Entry(window, textvariable = self.annualInterestRateVar, justify = RIGHT).grid(row = 1, column = 2) 17 self.numberOfYearsVar = StringVar() 18 Entry(window, textvariable = self.numberOfYearsVar, justify = RIGHT).grid(row = 2, column = 2) 19 self.loanAmountVar = StringVar() 20 Entry(window, textvariable = self.loanAmountVar, justify = RIGHT).grid(row = 3, column = 2) 21 22 self.monthlyPaymentVar = StringVar() 23 lblMonthlyPayment = Label(window, textvariable = self.monthlyPaymentVar).grid(row = 4, column = 2, sticky = E) 24 self.totalPaymentVar = StringVar() 25 lblTotalPayment = Label(window, textvariable = self.totalPaymentVar).grid(row = 5, column = 2, sticky = E) 26 27 btComputePayment = Button(window, text = "Compute payment", command = self.computePayment).grid(row = 6, column = 2, sticky = E) 28 29 window.mainloop() 30 31 def computePayment(self): 32 monthlyPayment = self.getMonthlyPayment(float(self.loanAmountVar.get()), float(self.annualInterestRateVar.get()) / 1200, int(self.numberOfYearsVar.get())) 33 self.monthlyPaymentVar.set(format(monthlyPayment, "10.2f")) 34 totalPayment = float(self.monthlyPaymentVar.get()) * 12 * int(self.numberOfYearsVar.get()) 35 self.totalPaymentVar.set(format(totalPayment, "10.2f")) 36 37 def getMonthlyPayment(self, loanAmount, monthlyInterestRate, numberOfYears): 38 monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / (1 + monthlyInterestRate) ** (numberOfYears * 12)) 39 return monthlyPayment 40 41 LoanCalculator()
33、35) 可通過monthlyPaymentVar.set(...)的方式,更改與小構件綁定的變量值從而更改小構件的參數(如顯示內容等)
16、23) Entry()與Label()皆使用textvariable的參數,不過Entry()中其指代用戶輸入的內容,Label()中指代標簽顯示的內容
many) 很多行當中都用到了Label(...).grid(...)的連寫方式,即小構件無需另外變量儲存,除非需要更改小構件參數(當然與其綁定的參數更改可以如上上行進行,但背景顏色、字體等參數仍然需要通過調用原小控件變量的方式進行更改,這種情況下就需要thing = Label(...).grid(...)的方式)
程序的運行結果如下:
8. 顯示圖像
# 鑒於書上程序實例中的圖片文件沒有獲取,這裏僅概述創建圖像對象與顯示的語法
創建:
myimage = PhotoImage(file = "yourpath.gif")
此時創建了一個圖片路徑為"yourpath.gif"的圖片對象myimage。
與Button、Checkbutton、Radiobutton的互動:
Button(frame, image = myimage) Checkbutton(frame, image = myimage) Radiobutton(frame, image = myimage)
此舉將創建包含myimage對象中的圖像的各種按鈕
與Canvas的互動:
canvas = Canvas(frame1)
canvas.create_image(width, height, image = myimage)
此舉將創建一個背景內容為myimage對象所含圖片的畫布,事實上一張畫布可以顯示多個圖像
Python Chapter 9: 使用Tkinter進行GUI程序設計 Part 2