Python Chapter 9: 使用Tkinter进行GUI程序设计 Part 3
Posted TBNR_Gabriel
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python Chapter 9: 使用Tkinter进行GUI程序设计 Part 3相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章