1. 程式人生 > >Python Chapter 9: 使用Tkinter進行GUI程序設計 Part 3

Python Chapter 9: 使用Tkinter進行GUI程序設計 Part 3

comm idt com title ear and elf 設計 port

9. 菜單

使用Menu類創建菜單欄和菜單,再使用add_command方法給菜單增加條目。

 1 # Program 9.13
 2 from tkinter import *
 3 
 4 class MenuDemo:
 5     def __init__(self):
 6         window = Tk()
 7         window.title("Menu Demo")
 8 
 9         menubar = Menu(window);
10         window.config(menu = menubar)
11 
12         operationMenu = Menu(menubar, tearoff = 0)
13 menubar.add_cascade(label = "Operation", menu = operationMenu) 14 operationMenu.add_command(label = "Add", command = self.add) 15 operationMenu.add_command(label = "Subtract", command = self.subtract) 16 operationMenu.add_separator() 17 operationMenu.add_command(label = "
Multiply", command = self.multiply) 18 operationMenu.add_command(label = "Divide", command = self.divide) 19 20 exitmenu = Menu(menubar, tearoff = 0) 21 menubar.add_cascade(label = "Exit", menu = exitmenu) 22 exitmenu.add_command(label = "Quit", command = window.quit)
23 24 frame0 = Frame(window) 25 frame0.grid(row = 1, column = 1, sticky = W) 26 27 plusImage = PhotoImage(file = "image/plus.gif") 28 minusImage = PhotoImage(file = "image/minus.gif") 29 timesImage = PhotoImage(file = "image/times.gif") 30 divideImage = PhotoImage(file = "image/divide.gif") 31 32 Button(frame0, image = plusImage, command = self.add).grid(row = 1, column = 1, sticky = W) 33 Button(frame0, image = minusImage, command = self.subtract).grid(row = 1, column = 2) 34 Button(frame0, image = timesImage, command = self.multiply).grid(row = 1, column = 3) 35 Button(frame0, image = divideImage, command = self.divide).grid(row = 1, column = 4) 36 37 frame1 = Frame(window) 38 frame1.grid(row = 2, column = 1, pady = 10) 39 Label(frame1, text = "Number 1:").pack(side = LEFT) 40 self.v1 = StringVar() 41 Entry(frame1, width = 5, textvariable = self.v1, justify = RIGHT).pack(side = LEFT) 42 Label(frame1, text = "Number 2:").pack(side = LEFT) 43 self.v2 = StringVar() 44 Entry(frame1, width = 5, textvariable = self.v2, justify = RIGHT).pack(side = LEFT) 45 Label(frame1, text = "Result:").pack(side = LEFT) 46 self.v3 = StringVar() 47 Entry(frame1, width = 5, textvariable = self.v3, justify = RIGHT).pack(side = LEFT) 48 49 frame2 = Frame(window) 50 frame2.grid(row = 3, column = 1, pady = 10, sticky = E) 51 Button(frame2, text = "Add", command = self.add).pack(side = LEFT) 52 Button(frame2, text = "Substract", command = self.subtract).pack(side = LEFT) 53 Button(frame2, text = "Multiply", command = self.multiply).pack(side = LEFT) 54 Button(frame2, text = "Divide", command = self.divide).pack(side = LEFT) 55 56 mainloop() 57 58 def add(self): 59 self.v3.set(eval(self.v1.get()) + eval(self.v2.get())) 60 61 def subtract(self): 62 self.v3.set(eval(self.v1.get()) - eval(self.v2.get())) 63 64 def multiply(self): 65 self.v3.set(eval(self.v1.get()) * eval(self.v2.get())) 66 67 def divide(self): 68 self.v3.set(eval(self.v1.get()) / eval(self.v2.get())) 69 70 MenuDemo()

9-10) Menu()創建一個菜單欄menubar,並通過window.config(menu = menubar)將此窗口的菜單欄設為menubar

12-13) Menu()創建一個菜單operationMenu,其中tearoff = 0代表此菜單不能被移出窗口外

    menubar.add_cascade(label, menu)在menubar菜單欄中新增標簽為label而內容為menu的菜單(有點菜單套菜單的意思)

14-18) operationMenu.add_command(label, command)往菜單operationMenu中添加命令(添加一個選項),標簽為label命令為command

    add_separator()增加一個橫線標識區分

22) window.quit退出窗口結束程序

全) 全程序有frame0、frame1、frame2用來存放三行內容

程序運行結果如下:

技術分享圖片

Python Chapter 9: 使用Tkinter進行GUI程序設計 Part 3