要更新的Tkinter Optionmenu-options列表(选择后,应从选项中删除该选项)

时间:2016-02-11 05:16:30

标签: python tkinter

选项菜单有3个选项“a”,“b”,“c”。假设用户为第一个选项菜单选择“b”。当他点击添加按钮时,第二个选项菜单应该只显示两个选项“a”,“c”,因为他已经选择了选项“b”

我的代码显示三个选项,无论选择的选项/选项如何。有什么出路吗

import tkinter
from tkinter import *
class Window(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.master=master
        self.func()

def func(self):
    self.count=0
    self.op_row=0
    button=Button(self.master,text="Add",command= self.func_op)
    button.grid(column=0,row=0)
    label=Label(self,text="Welcome")
    label.grid(column=0,row=0)

    def func_op(self):
        self.count=self.count+1
        self.op_row=self.op_row+1
        self.var=StringVar()
        options=["a","b","c"]
        op=OptionMenu(self.master,self.var,*options)
        op.grid(column=0,row=self.op_row)

if __name__ == "__main__":
    root = Tk()
    aplication = Window(root)
    root.mainloop()   

2 个答案:

答案 0 :(得分:2)

The optionmenu isn't designed to have elements removed when they are selected. It's specifically designed to display the item that was selected.

If you want to let the user pick something from a menu and then remove the item from the menu, you should just use a menubutton and a menu. That is exactly what an OptionMenu is, except that it has additional behavior that you explicitly don't want to use.

Here's a simple example:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.menubutton = tk.Menubutton(self, text="Pick an option", indicatoron=True, 
                                        borderwidth=1, relief="raised")
        self.menu = tk.Menu(self.menubutton, tearoff=False)
        self.menubutton.configure(menu=self.menu)
        for choice in ("a", "b", "c"):
            self.menu.add_command(label=choice, 
                                  command=lambda option=choice: self.set_option(option))

        self.text = tk.Text(self)
        self.menubutton.pack(side="top")
        self.text.pack(side="top", fill="both", expand=True)

    def set_option(self, option):
        self.text.insert("end", "you have chosen %s\n" % option)
        self.menu.delete(option)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

Your users might find it very strange and confusing to see items disappear from a menu. It your main goal is to simply prevent the user from picking the same option twice you can simply disable the item once it is chose with something like this in place of the delete statement:

self.menu.entryconfigure(option, state="disabled")

答案 1 :(得分:1)

这应该可以解决问题。现在Name菜单有3个元素a,b,c,如果按下button,所选的一个将被“添加”(打印到控制台)并将从列表中消失。

import sys
if sys.version_info[0] >= 3:
    import tkinter as tk
else:
    import Tkinter as tk

class App(tk.Frame):


    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.dict = ['a','b','c']
        self.variable_a = tk.StringVar()
        self.optionmenu_a = tk.OptionMenu(self, self.variable_a, *self.dict)
        tk.Button(self, text="Add", command=self.func).pack()
        self.optionmenu_a.pack()
        self.pack()

    def func(self):
        menu = self.optionmenu_a["menu"]
        print self.variable_a.get() + " added"
        menu.delete(self.dict.index(self.variable_a.get()))



if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    app.mainloop()

据我所知,你不能删除最后一个元素。但是如果你删除最后一个元素,你可以删除整个选项菜单。

编辑:根据OP评论编辑,并编辑了代码