用tkinter Menu写一个页面
时间: 2023-12-09 19:00:01 浏览: 75
好的,下面是一个简单的例子,可以创建一个带有菜单栏的窗口:
```python
import tkinter as tk
root = tk.Tk()
# 创建菜单栏
menu_bar = tk.Menu(root)
# 创建菜单选项
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="New")
file_menu.add_command(label="Open")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
edit_menu = tk.Menu(menu_bar, tearoff=0)
edit_menu.add_command(label="Cut")
edit_menu.add_command(label="Copy")
edit_menu.add_command(label="Paste")
# 将菜单选项添加到菜单栏
menu_bar.add_cascade(label="File", menu=file_menu)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
# 将菜单栏添加到窗口中
root.config(menu=menu_bar)
root.mainloop()
```
这个例子创建了一个带有“File”和“Edit”菜单的窗口。在“File”菜单中,有“New”、“Open”两个选项,以及一个分隔线和“Exit”选项,当点击“Exit”时,会退出程序。在“Edit”菜单中,有“Cut”、“Copy”、“Paste”三个选项。
阅读全文
相关推荐


















