Tóm tắt: Trong hướng dẫn này, bạn sẽ học cách tạo thanh menu Tkinter, thêm menu vào thanh menu và thêm các mục menu vào mỗi menu.: in this tutorial, you’ll learn how to create a Tkinter menu bar, add menus to the menu bar, and add menu items to each menu.
Khi một ứng dụng chứa rất nhiều chức năng, bạn cần sử dụng các menu để sắp xếp chúng để điều hướng dễ dàng hơn.
Thông thường, bạn sử dụng một menu để nhóm các hoạt động liên quan chặt chẽ. Ví dụ: bạn có thể tìm thấy menu tệp trong hầu hết các trình soạn thảo văn bản.
Tkinter tự nhiên hỗ trợ menu. Nó hiển thị các menu với cái nhìn và cảm giác của nền tảng đích mà chương trình chạy, ví dụ: Windows, MacOS và Linux.
Tạo một menu đơn giản
Đầu tiên, tạo cửa sổ
0 và đặt tiêu đề của nó thànhCode language: Python [python]
menubar = Menu[root] root.config[menu=menubar]
1:Code language: Python [python]
menubar = Menu[root] root.config[menu=menubar]
Code language: Python [python]
root = tk.Tk[] root.title['Menu Demo']
Thứ hai, tạo một thanh menu và gán nó cho tùy chọn
2 của cửa sổCode language: Python [python]
menubar = Menu[root] root.config[menu=menubar]
0:Code language: Python [python]
menubar = Menu[root] root.config[menu=menubar]
Code language: Python [python]
menubar = Menu[root] root.config[menu=menubar]
Lưu ý rằng mỗi cửa sổ cấp cao nhất chỉ có thể có một thanh menu.
Thứ ba, tạo một menu tệp có thùng chứa là
4:File menu whose container is theCode language: Python [python]
menubar = Menu[root] root.config[menu=menubar]
4:Code language: Python [python]
menubar = Menu[root] root.config[menu=menubar]
Code language: Python [python]
file_menu = Menu[menubar]
Thứ tư, thêm một mục menu vào
5:
menubar = Menu[root] root.config[menu=menubar]
Code language: Python [python]
Code language: Python [python]
file_menu.add_command[ label='Exit', command=root.destroy, ]
Trong ví dụ này, nhãn của mục menu là
6.Code language: Python [python]
menubar = Menu[root] root.config[menu=menubar]
Khi bạn nhấp vào mục menu
6, Python sẽ tự động gọi phương thứcCode language: Python [python]
menubar = Menu[root] root.config[menu=menubar]
8 để đóng cửa sổCode language: Python [python]
menubar = Menu[root] root.config[menu=menubar]
0.Code language: Python [python]
menubar = Menu[root] root.config[menu=menubar]
Cuối cùng, thêm menu
0 vào Menubar:Code language: Python [python]
file_menu = Menu[menubar]
Code language: Python [python]
menubar.add_cascade[ label="File", menu=file_menu, underline=0 ]
Tùy chọn
1 cho phép bạn tạo một phím tắt. Nó chỉ định vị trí ký tự nên được gạch chân.Code language: Python [python]
file_menu = Menu[menubar]
Lưu ý rằng vị trí bắt đầu từ không. Trong ví dụ này, chúng tôi chỉ định nó là ký tự đầu tiên là
2. Và bạn có thể chọn nó bằng cách sử dụng phím tắtCode language: Python [python]
file_menu = Menu[menubar]
3.Code language: Python [python]
file_menu = Menu[menubar]
Đặt nó tất cả cùng nhau:
import tkinter as tk from tkinter import Menu # root window root = tk.Tk[] root.title['Menu Demo'] # create a menubar menubar = Menu[root] root.config[menu=menubar] # create a menu file_menu = Menu[menubar] # add a menu item to the menu file_menu.add_command[ label='Exit', command=root.destroy ] # add the File menu to the menubar menubar.add_cascade[ label="File", menu=file_menu ] root.mainloop[]
Code language: Python [python]
Output:
Theo mặc định, Tkinter thêm một đường đứt nét trước mục menu đầu tiên. Khi bạn nhấp vào đường đứt nét, cửa sổ chính sẽ tách menu ra khỏi nó như thế này:
Để xóa đường đứt nét, bạn có thể đặt thuộc tính
4 của menu thànhCode language: Python [python]
file_menu = Menu[menubar]
5:Code language: Python [python]
file_menu = Menu[menubar]
Code language: Python [python]
file_menu = Menu[menubar, tearoff=False]
Output:
Tạo một menu phức tạp hơn
Chương trình sau đây minh họa cách tạo một thanh menu, thêm tệp và trợ giúp menu vào thanh menu. Ngoài ra, nó thêm nhiều mục menu vào các menu sau:File and Help menus to the menu bar. Also, it adds multiple menu items to these menus:
Code language: Python [python]
from tkinter import Tk, Frame, Menu # root window root = Tk[] root.geometry['320x150'] root.title['Menu Demo'] # create a menubar menubar = Menu[root] root.config[menu=menubar] # create the file_menu file_menu = Menu[ menubar, tearoff=0 ] # add menu items to the File menu file_menu.add_command[label='New'] file_menu.add_command[label='Open...'] file_menu.add_command[label='Close'] file_menu.add_separator[] # add Exit menu item file_menu.add_command[ label='Exit', command=root.destroy ] # add the File menu to the menubar menubar.add_cascade[ label="File", menu=file_menu ] # create the Help menu help_menu = Menu[ menubar, tearoff=0 ] help_menu.add_command[label='Welcome'] help_menu.add_command[label='About...'] # add the Help menu to the menubar menubar.add_cascade[ label="Help", menu=help_menu ] root.mainloop[]
Output:
Câu lệnh mới duy nhất trong chương trình này là sử dụng phương thức
6 để thêm một dấu tách vào menu.Code language: Python [python]
file_menu = Menu[menubar]
Thêm một menu con
Chương trình sau đây thêm mục menu
7 vào menuCode language: Python [python]
file_menu = Menu[menubar]
0 và tạo một menu con liên kết mục menu mới:Code language: Python [python]
file_menu = Menu[menubar]
from tkinter import Tk, Menu # root window root = Tk[] root.geometry['320x150'] root.title['Menu Demo'] # create a menubar menubar = Menu[root] root.config[menu=menubar] # create the file_menu file_menu = Menu[ menubar, tearoff=0 ] # add menu items to the File menu file_menu.add_command[label='New'] file_menu.add_command[label='Open...'] file_menu.add_command[label='Close'] file_menu.add_separator[] # add a submenu sub_menu = Menu[file_menu, tearoff=0] sub_menu.add_command[label='Keyboard Shortcuts'] sub_menu.add_command[label='Color Themes'] # add the File menu to the menubar file_menu.add_cascade[ label="Preferences", menu=sub_menu ] # add Exit menu item file_menu.add_separator[] file_menu.add_command[ label='Exit', command=root.destroy ] menubar.add_cascade[ label="File", menu=file_menu, underline=0 ] # create the Help menu help_menu = Menu[ menubar, tearoff=0 ] help_menu.add_command[label='Welcome'] help_menu.add_command[label='About...'] # add the Help menu to the menubar menubar.add_cascade[ label="Help", menu=help_menu, underline=0 ] root.mainloop[]
Code language: Python [python]
Output:
Làm thế nào nó hoạt động.
Mã sau đây thêm một menu con vào menu
0 và liên kết các menu con với mục menuCode language: Python [python]
file_menu = Menu[menubar]
7:Code language: Python [python]
file_menu = Menu[menubar]
Code language: Python [python]
# add a submenu sub_menu = Menu[file_menu, tearoff=0] sub_menu.add_command[label='Keyboard Shortcuts'] sub_menu.add_command[label='Color Themes'] # add the File menu to the menubar file_menu.add_cascade[ label="Preferences", menu=sub_menu ]
Bản tóm tắt
- Sử dụng
1 để tạo menu mới,
Code language: Python [python]file_menu.add_command[ label='Exit', command=root.destroy, ]
- Sử dụng phương thức
2 để thêm một mục menu vào menu.
Code language: Python [python]file_menu.add_command[ label='Exit', command=root.destroy, ]
- Sử dụng
3 để thêm
Code language: Python [python]file_menu.add_command[ label='Exit', command=root.destroy, ]
2 vào
Code language: Python [python]menubar = Menu[root] root.config[menu=menubar]
4.
Code language: Python [python]menubar = Menu[root] root.config[menu=menubar]
- Sử dụng
6 để thêm một menu con vào
Code language: Python [python]file_menu.add_command[ label='Exit', command=root.destroy, ]
2.
Code language: Python [python]menubar = Menu[root] root.config[menu=menubar]
Bạn có thấy hướng dẫn này hữu ích không?