利用python和tkinter在页面中创建菜单栏
以下是一个简单的Python Tkinter程序,可以在页面中创建菜单栏:
import tkinter as tk
def do_nothing():
pass
root = tk.Tk()
# create a menu bar
menu_bar = tk.Menu(root)
root.config(menu=menu_bar)
# create a file menu
file_menu = tk.Menu(menu_bar)
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=do_nothing)
file_menu.add_command(label="Open", command=do_nothing)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
# create an edit menu
edit_menu = tk.Menu(menu_bar)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut", command=do_nothing)
edit_menu.add_command(label="Copy", command=do_nothing)
edit_menu.add_command(label="Paste", command=do_nothing)
# create a help menu
help_menu = tk.Menu(menu_bar)
menu_bar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=do_nothing)
root.mainloop()
这个程序创建了一个窗口,并在窗口中创建了一个菜单栏,其中包含了“File”、“Edit”和“Help”三个菜单。在这些菜单中,我们可以添加不同的菜单项和子菜单项。在这个例子中,我们只是添加了一些占位符命令“do_nothing”,当我们点击这些菜单项时,什么也不会发生。你可以根据自己的需求修改这些菜单项的命令
原文地址: https://www.cveoy.top/t/topic/fmpg 著作权归作者所有。请勿转载和采集!