Hướng dẫn how do you create a submenu in python? - làm thế nào để bạn tạo một menu con trong python?

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ổ

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)
0 và đặt tiêu đề của nó thành

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)
1:

root = tk.Tk() root.title('Menu Demo')

Code language: Python (python)

Thứ hai, tạo một thanh menu và gán nó cho tùy chọn

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)
2 của cửa sổ

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)
0:

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)

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à

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)
4:File menu whose container is the

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)
4:

file_menu = Menu(menubar)

Code language: Python (python)

Thứ tư, thêm một mục menu vào

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)
5:

file_menu.add_command( label='Exit', command=root.destroy, )

Code language: Python (python)

Trong ví dụ này, nhãn của mục menu là

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)
6.

Khi bạn nhấp vào mục menu

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)
6, Python sẽ tự động gọi phương thức

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)
8 để đóng cửa sổ

menubar = Menu(root) root.config(menu=menubar)

Code language: Python (python)
0.

Cuối cùng, thêm menu

file_menu = Menu(menubar)

Code language: Python (python)
0 vào Menubar:

menubar.add_cascade( label="File", menu=file_menu, underline=0 )

Code language: Python (python)

Tùy chọn

file_menu = Menu(menubar)

Code language: Python (python)
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.

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à

file_menu = Menu(menubar)

Code language: Python (python)
2. Và bạn có thể chọn nó bằng cách sử dụng phím tắt

file_menu = Menu(menubar)

Code language: Python (python)
3.

Đặ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:

Hướng dẫn how do you create a submenu in python? - làm thế nào để bạn tạo một menu con trong python?

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:

Hướng dẫn how do you create a submenu in python? - làm thế nào để bạn tạo một menu con trong python?

Để xóa đường đứt nét, bạn có thể đặt thuộc tính

file_menu = Menu(menubar)

Code language: Python (python)
4 của menu thành

file_menu = Menu(menubar)

Code language: Python (python)
5:

file_menu = Menu(menubar, tearoff=False)

Code language: Python (python)

Output:

Hướng dẫn how do you create a submenu in python? - làm thế nào để bạn tạo một menu con trong python?

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:

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()

Code language: Python (python)

Output:

Hướng dẫn how do you create a submenu in python? - làm thế nào để bạn tạo một menu con trong python?

Câu lệnh mới duy nhất trong chương trình này là sử dụng phương thức

file_menu = Menu(menubar)

Code language: Python (python)
6 để thêm một dấu tách vào menu.

Thêm một menu con

Chương trình sau đây thêm mục menu

file_menu = Menu(menubar)

Code language: Python (python)
7 vào menu

file_menu = Menu(menubar)

Code language: Python (python)
0 và tạo một menu con liên kết mục menu mới:

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:

Hướng dẫn how do you create a submenu in python? - làm thế nào để bạn tạo một menu con trong python?

Làm thế nào nó hoạt động.

Mã sau đây thêm một menu con vào menu

file_menu = Menu(menubar)

Code language: Python (python)
0 và liên kết các menu con với mục menu

file_menu = Menu(menubar)

Code language: Python (python)
7:

# 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 )

Code language: Python (python)

Bản tóm tắt

  • Sử dụng

    file_menu.add_command( label='Exit', command=root.destroy, )

    Code language: Python (python)
    1 để tạo menu mới,
  • Sử dụng phương thức

    file_menu.add_command( label='Exit', command=root.destroy, )

    Code language: Python (python)
    2 để thêm một mục menu vào menu.
  • Sử dụng

    file_menu.add_command( label='Exit', command=root.destroy, )

    Code language: Python (python)
    3 để thêm

    menubar = Menu(root) root.config(menu=menubar)

    Code language: Python (python)
    2 vào

    menubar = Menu(root) root.config(menu=menubar)

    Code language: Python (python)
    4.
  • Sử dụng

    file_menu.add_command( label='Exit', command=root.destroy, )

    Code language: Python (python)
    6 để thêm một menu con vào

    menubar = Menu(root) root.config(menu=menubar)

    Code language: Python (python)
    2.

Bạn có thấy hướng dẫn này hữu ích không?

Làm thế nào để bạn tạo một danh sách thả xuống trong Python?

Xây dựng một chương trình sơn với Tkinter và Python Trong trường hợp này, chúng ta có thể sử dụng chức năng Tkinter OptionMenu (WIN, MENU_TO_SET, Tùy chọn).Đầu tiên, chúng tôi sẽ khởi tạo một đối tượng của StringVar (), sau đó chúng tôi sẽ đặt giá trị ban đầu của menu thả xuống.use the Tkinter OptionMenu(win, menu_to_set, options) function. First, we will instantiate an object of StringVar(), then we will set the initial value of the dropdown menu.

Sự khác biệt giữa menu và menu con là gì?

Một menu con hoặc menu xếp tầng là một menu thứ cấp được hiển thị theo yêu cầu từ trong menu.Chúng được chỉ định bởi một mũi tên ở cuối nhãn menu con.Một mục menu là một lệnh hoặc tùy chọn riêng lẻ trong menu.. They are indicated by an arrow at the end of the submenu label. A menu item is an individual command or option within a menu.

Làm cách nào để tạo một menu bật lên trong Python?

Một menu bật lên có thể được tạo bằng cách khởi tạo TK_POPUP (X_ROOT, Y_ROOT, FALSE) để đảm bảo rằng menu có thể nhìn thấy trên màn hình.Bây giờ, chúng tôi sẽ thêm một sự kiện có thể được kích hoạt thông qua nút chuột (nhấp chuột phải).Phương thức Grab_Release () đặt phát hành nút chuột để hủy đặt menu bật lên.initializing tk_popup(x_root,y_root, False) which ensures that the menu is visible on the screen. Now, we will add an event which can be triggered through the Mouse Button (Right Click). The grab_release() method sets the mouse button release to unset the popup menu.