Create Python Menu Bar with Tkinter
In Python, the tkinter module can be used to create a menu bar. Below is a simple example code to show how to create a GUI interface with a menu bar.
import tkinter as tk
def hello():
print("Hello!")
root = tk.Tk()
# 创建菜单栏
menu_bar = tk.Menu(root)
# 创建菜单
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Open", command=hello)
file_menu.add_command(label="Save", command=hello)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
# 将菜单添加到菜单栏
menu_bar.add_cascade(label="File", menu=file_menu)
# 将菜单栏添加到主窗口
root.config(menu=menu_bar)
root.mainloop()
In this example, we first created a main window called root, then created a menu bar called menu_bar, followed by creating a file menu called file_menu and adding some commands. Finally, we added the file menu to the menu bar, and the menu bar to the main window. Running this code, you will see a GUI interface with a menu bar.