python-3.x 我们能改变菜单的高度吗?

xqkwcwgp  于 2023-04-13  发布在  Python
关注(0)|答案(1)|浏览(130)

我在tkinter弹出菜单小部件中添加了很多选项,但它填满了整个屏幕高度(如图所示)。我想限制tkinter菜单中显示的选项数量,如tkinter组合框的列表框高度(可以通过传递height参数来更改)

菜单小部件有什么方法可以做到吗?请帮忙!
示例:

from tkinter import Tk, Menu

# root window
root = Tk()

# create a menubar
menubar = Menu(root)
root.config(menu=menubar)

# create the file_menu
file_menu = Menu(menubar, tearoff=0, bg="grey50", activebackground="blue")

# add lots of menu items to the File menu
for i in range(1,100):
    file_menu.add_command(label=i)

# add the File menu to the menubar
menubar.add_cascade(label="Menu",menu=file_menu)

root.mainloop()

f4t66c6m

f4t66c6m1#

下面是一个使用columnbreaktk.Menu示例。
生成100个随机数并显示在10x10面板上。

import tkinter as tk
from random import randrange as rnd

kwd = dict(tearoff = 0, font = "Consolas 9 normal")

class menutest(tk.Tk):

    def __init__(self):

        super().__init__()

        self.menubar = tk.Menu(self, **kwd)
        self.numenu = tk.Menu(self.menubar, **kwd)

        for a in range(10):
            for b in range(10):

                c = rnd(50000)
                self.numenu.add_command(
                    label = f"{c: <6}", columnbreak = int((b == 0)),
                    command = lambda c = c: self.extract(c), hidemargin = True)

        self.menubar.add_cascade(
            label = f"{'◆ Data': <9}",
            menu = self.numenu, underline = 0, hidemargin = True)

        self.bind("<ButtonPress-3>", self.popup)
        self.focus_set()

    def popup(self, event):
        self.menubar.post(event.x_root, event.y_root)

    def extract(self, c):
        print(c)

if __name__ == "__main__":

    app = menutest()
    app.mainloop()

相关问题