Repository navigation
Expand file tree
/
Copy pathmenu.py
More file actions
executable file
·65 lines (54 loc) · 2.04 KB
/
Copy pathmenu.py
File metadata and controls
executable file
·65 lines (54 loc) · 2.04 KB
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
#coding:utf-8
try:
from tkinter import Frame,Label,Button,Menu
except ImportError:
from Tkinter import Frame,Label,Button,Menu
class Application(Frame):
def __init__(self, master=None,):
Frame.__init__(self, master)
#窗口大小位置
self.pack()
self.createWidgets()
self.creatMenu()
def creatMenu(self):
#主菜单
menubar = Menu(self)
#子菜单
menufile = Menu(menubar)
for item in ["新建","打开","保存","另存为"]:
menufile.add_radiobutton(label = item)
menuedit = Menu(menubar)
for item in ["复制","黏贴","剪切"]:
menuedit.add_checkbutton(label = item)
#子菜单与主菜单关联
for name,submenu in zip(["文件","编辑"],[menufile,menuedit]):
menubar.add_cascade(label=name,menu=submenu)
#最关键的一步,主菜单与app关联
self.master.config(menu=menubar)
#右键菜单
menu = Menu(self.master)
for i in ('One', 'Two', 'Three'):
menu.add_command(label=i)
#插入分割线
menu.add_separator()
for i in ('1', '2', '3'):
menu.add_command(label=i)
#绑定鼠标右键呼出
if (self.master.tk.call('tk', 'windowingsystem')=='aqua'):
self.master.bind('<2>', lambda e: menu.post(e.x_root, e.y_root))
self.master.bind('<Control-1>', lambda e: menu.post(e.x_root, e.y_root))
else:
self.master.bind('<3>', lambda e: menu.post(e.x_root, e.y_root))
def createWidgets(self):
self.helloLabel = Label(self, text='Hello, world!')
self.helloLabel["background"] ="green"
self.helloLabel.pack()
self.quitButton = Button(self, text='Quit',fg="red", command=self.quit)
self.quitButton.pack()
if __name__ =="__main__":
app = Application()
# 设置窗口标题:
app.master.title('Hello World')
#app.master.geometry("600x400+100+400")#长x宽+x+y
# 主消息循环:
app.mainloop()