Python 入門指南 5.0
exercise3303.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | # 從標準程式庫引入 Tk from tkinter import * # 從 tkinter 引入 ttk from tkinter import ttk # 練習用的視窗類別 class Application(ttk.Frame): def __init__(self, master): ttk.Frame.__init__(self, master) self.winfo_toplevel().title("練習的視窗") self.grid() self.createWidgets() self.style = ttk.Style() self.style.theme_use("alt") for child in self.winfo_children(): child.grid_configure(padx=5, pady=3) def createWidgets(self): self.value1 = StringVar() self.value2 = StringVar() self.value3 = StringVar() self.value4 = StringVar() self.value1.set("") self.value2.set("") self.value3.set("") self.value4.set("") self.checkbutton1 = Checkbutton(self) self.checkbutton1["text"] = "選我1" self.checkbutton1["width"] = 20 self.checkbutton1["variable"] = self.value1 self.checkbutton1["offvalue"] = "" self.checkbutton1["command"] = self.do_something self.checkbutton1.grid(row=0, column=0, sticky=N) self.checkbutton2 = Checkbutton(self) self.checkbutton2["text"] = "選我2" self.checkbutton2["width"] = 20 self.checkbutton2["variable"] = self.value2 self.checkbutton2["offvalue"] = "" self.checkbutton2["command"] = self.do_something self.checkbutton2.grid(row=1, column=0, sticky=N) self.checkbutton3 = Checkbutton(self) self.checkbutton3["text"] = "選我3" self.checkbutton3["width"] = 20 self.checkbutton3["variable"] = self.value3 self.checkbutton3["offvalue"] = "" self.checkbutton3["command"] = self.do_something self.checkbutton3.grid(row=2, column=0, sticky=N) self.checkbutton4 = Checkbutton(self) self.checkbutton4["text"] = "選我4" self.checkbutton4["width"] = 20 self.checkbutton4["variable"] = self.value4 self.checkbutton4["offvalue"] = "" self.checkbutton4["command"] = self.do_something self.checkbutton4.grid(row=3, column=0, sticky=N) self.label = Label(self) self.label["text"] = "" self.label["width"] = 20 self.label.grid(row=4, column=0, sticky=N) def do_something(self): result = "" if self.value1.get(): result += "選我1 " if self.value2.get(): result += "選我2 " if self.value3.get(): result += "選我3 " if self.value4.get(): result += "選我4 " self.label["text"] = result # 執行部分 if __name__ == '__main__': # 建立 Tk 應用物件 root = Tk() # 建立視窗物件 app = Application(master=root) # 呼叫維持視窗運作的 mainloop() root.mainloop() # 檔名: exercise3303.py # 說明:《Python入門指南》的練習 # 網站: http://kaiching.org # 作者: 張凱慶 # 時間: 2023 年 11 月 |