Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将滚动条插入菜单?_Python_Tkinter_Widget - Fatal编程技术网

Python 如何将滚动条插入菜单?

Python 如何将滚动条插入菜单?,python,tkinter,widget,Python,Tkinter,Widget,我想向tkinter中的菜单小部件添加另一个小部件,在本例中是scale小部件。 现在,我看到的唯一解决方案是创建一个新命令,用scale小部件打开一个新窗口,或者在别处创建scale小部件。这两个对我来说都不太吸引人 欢迎您提供任何存档方法:)您不能在其中添加滚动条,但是。这是一种令人讨厌的方式,也许很难理解,但我可以试着解释 注意正如Bryan在链接线程中提到的,这似乎是一个仅限Windows的解决方案 import tkinter as tk def my_first_function

我想向tkinter中的菜单小部件添加另一个小部件,在本例中是scale小部件。 现在,我看到的唯一解决方案是创建一个新命令,用scale小部件打开一个新窗口,或者在别处创建scale小部件。这两个对我来说都不太吸引人


欢迎您提供任何存档方法:)

您不能在其中添加滚动条,但是。这是一种令人讨厌的方式,也许很难理解,但我可以试着解释

注意正如Bryan在链接线程中提到的,这似乎是一个仅限Windows的解决方案

import tkinter as tk


def my_first_function():
    print('first')

def my_second_function():
    print('second')

def check_for_scroll(event):
    check = root.call(event.widget, "index","active")
    if check == 0: #index of button up
        scroll_up()
        root.after(100,lambda e=event:check_for_scroll(e)) # check again after 100ms
    if check == file_menu.index('end'):
        scroll_down()
        root.after(100,lambda e=event:check_for_scroll(e))

def scroll_up():
    index_of_first_command=1
    index_of_last_command=1
    label_of_last_command = file_menu.entrycget(index_of_first_command, 'label')
    try:
        for i, k in enumerate(dict_of_commands):
            if k == label_of_last_command:
                previous_command_label = list(dict_of_commands)[i-1]
                previous_command = list(dict_of_commands.values())[i-1]
                if i != 0: #avoid to get the last as first
                    file_menu.delete(index_of_first_command) #first before pull down button
                    file_menu.insert_command(index_of_first_command,
                                             label=previous_command_label,
                                             command=previous_command)
    except Exception as e:
        print(e)

def scroll_down():
    index_of_first_command=1
    index_of_last_command=1
    label_of_last_command = file_menu.entrycget(index_of_last_command, 'label')
    try:
        for i, k in enumerate(dict_of_commands):
            if k == label_of_last_command:
                next_command_label = list(dict_of_commands)[i+1]
                next_command = list(dict_of_commands.values())[i+1]

                file_menu.delete(index_of_first_command) #first after pull up button
                file_menu.insert_command(index_of_last_command,
                                         label=next_command_label,
                                         command=next_command)
    except:
        pass

space = '         '
dict_of_commands = {'first' : my_first_function,
                    'second': my_second_function}

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

file_menu = tk.Menu(menubar,tearoff=0)
menubar.add_cascade(label='File', menu=file_menu)
file_menu.bind('<<MenuSelect>>', check_for_scroll)

file_menu.add_command(label=space+u'\u25B2'+space, font=["Arial", 8,'bold'])

file_menu.add_command(label='first', command=my_first_function)

file_menu.add_command(label=space+u'\u25BC'+space, font=["Arial", 8,'bold'])


root.mainloop()
这句话对你来说很重要:

file_menu.bind('<<MenuSelect>>', check_for_scroll)
下一行检查触发事件的命令的索引。有了这个,我们用箭头来检查我们感兴趣的按钮是第一个还是最后一个

check = root.call(event.widget, "index","active")
如果是第一个示例,则执行此代码:

if check == 0: #index of button up
        scroll_up()
        root.after(100,lambda e=event:check_for_scroll(e)) # check again after 100ms
它调用/触发函数scroll_up,然后使用tkinter的after方法重新触发自身,就像循环一样。向上滚动功能的构建方式与向下滚动功能的构建方式相同,只是方向相反。让我们仔细看看:

def scroll_up():
    index_of_first_command=1
    index_of_last_command=1
    label_of_last_command = file_menu.entrycget(index_of_first_command, 'label')
    try:
        for i, k in enumerate(dict_of_commands):
            if k == label_of_last_command:
                previous_command_label = list(dict_of_commands)[i-1]
                previous_command = list(dict_of_commands.values())[i-1]
                if i != 0: #avoid to get the last as first
                    file_menu.delete(index_of_first_command) #first before pull down button
                    file_menu.insert_command(index_of_first_command,
                                             label=previous_command_label,
                                             command=previous_command)
    except Exception as e:
        print(e)
在这个函数中,我们需要知道命令的第一个和最后一个位置,因为我们要删除一个命令,然后在该位置/索引上插入另一个命令。为了实现这一点,我创建了一个字典,其中包含tkinter命令项的标签和函数,如下所示。(这可以动态创建,但让我们保留它以供其他问题使用)

因此,我们在函数中迭代这个枚举/索引字典,并检查k/key/label是否是我们感兴趣的项。如果为true,我们将通过列出字典键来获取上一个_命令,并通过此行获取解压前的键:

next_command_label = list(dict_of_commands)[i+1]
next_command = list(dict_of_commands.values())[i+1]
与此行的字典值类似:

next_command_label = list(dict_of_commands)[i+1]
next_command = list(dict_of_commands.values())[i+1]
毕竟,我们可以删除一个,然后在希望的地方插入一个:

file_menu.delete(index_of_first_command) #first after pull up button
file_menu.insert_command(index_of_last_command,
                         label=next_command_label,
                         command=next_command)

我知道这段代码可以改进很多,但它看起来很难理解。所以请不要评判我

我希望这能解决你的问题,即使这不是你想要的方式。但这段代码避免您编写自己的菜单栏


如果我的答案中还有问题,请告诉我。

请编辑您的问题,以包含到目前为止您拥有的代码的最低示例。您可以添加命令、子菜单、分隔符、单选按钮或复选框。缩放不是菜单widget的选项。您不能向菜单添加滚动条、缩放或任何其他小部件。非常感谢。这不完全是我的想法,但我可能会让它以这种方式工作。不客气:)我希望它能带来令人满意的解决方案。编写这个代码很有趣,谢谢你的提问。
file_menu.delete(index_of_first_command) #first after pull up button
file_menu.insert_command(index_of_last_command,
                         label=next_command_label,
                         command=next_command)