简体   繁体   English

新选择后将标记保留在下拉菜单 tkinter 中

[英]Keep marker in dropdown menu tkinter after new selection

Is there any way to keep the marker in a tkinter dropdown menu (OptionMenu)?有没有办法将标记保留在 tkinter 下拉菜单(OptionMenu)中? Let's say I have a dropdown menu having 5 entries ("A", "B", "C", "D", "E").假设我有一个包含 5 个条目(“A”、“B”、“C”、“D”、“E”)的下拉菜单。 At the beginning "A" is selected.开始时选择“A”。 When I click the dropdown menu and change to eg "C" and reopen it the blue marker disappeared.当我单击下拉菜单并更改为例如“C”并重新打开它时,蓝色标记消失了。 However, I really would like to see in the dropdown list that C was selected (marked in blue).但是,我真的很想在下拉列表中看到 C 被选中(标记为蓝色)。 Especially when having a list of more than 10 elements, it is much faster to recognize by eye which value was selected and how to proceed.尤其是当列表包含 10 个以上的元素时,通过肉眼识别选择了哪个值以及如何进行要快得多。

在此处输入图片说明 在此处输入图片说明

You can use add_checkbutton , which adds checkable commands to the menu.您可以使用add_checkbutton ,它将可检查的命令添加到菜单中。 I think this is exactly what you need.我认为这正是你所需要的。 This means you should use a normal Menu instead of a OptionMenu...这意味着您应该使用普通菜单而不是 OptionMenu ...

Question : Keep marker in OptionMenu dropdown, after new selection.问题:在新选择后,在OptionMenu下拉菜单中保留标记。

You can do this by using menu.postcommand and a StringVar observer.你可以通过使用menu.postcommand和一个StringVar观察者来做到这一点。
To set the marker, use .entryconfig(... with the activeforeground/activebackground value.要设置标记,请使用.entryconfig(...activeforeground/activebackground值。
在此处输入图片说明



import tkinter as tk


class OptionMenu(tk.OptionMenu):
    def __init__(self, parent, *options, **kwargs):
        self.selected = tk.StringVar(parent, '')  # None default value
        super().__init__(parent, self.selected, *options, **kwargs)

        self.options = options
        menu = self['menu']
        menu.configure(activeforeground='white', activebackground='blue',
                       postcommand=self.postcommand)
        self.normal = {'foreground': menu.cget('foreground'),
                       'background': menu.cget('background')}
        self.active = {'foreground': menu.cget('activeforeground'),
                       'background': menu.cget('activebackground')}

        self.selected.trace("w", self.on_selected)
        self.index = 0

    def get(self):
        return self.selected.get()

    def on_selected(self, *_):
        self['menu'].entryconfig(self.index, **self.normal)
        self.index = self.options.index(self.get())

    def postcommand(self):
        self['menu'].activate(self.index)
        self['menu'].entryconfig(self.index, **self.active)

Usage :用法

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        options = ['   A   ', '   B   ', '   C   ', '   D   ', '   E   ', '   F   ']
        om = OptionMenu(self, *options)
        om.grid()


if __name__ == "__main__":
    App().mainloop()

Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6用 Python 测试:3.5 - 'TclVersion':8.6 'TkVersion':8.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM