Python Tkinter单选按钮缩小用户输入范围

3

我是编程、Python和Tkinter的新手,我想要一个好的解决方案(也许使用state=DISABLED?)来限制用户根据他们选择的按钮的选项。

我的现有代码:

from Tkinter import *

master = Tk()

def ok():
    master.destroy()  

v1 = IntVar()
v2 = IntVar()
v3 = IntVar()
v4 = IntVar()
v5 = IntVar()

Label(master, text="""Which Method do you want to run?""",justify = LEFT, padx = 20).pack()
Radiobutton(master, text="Positive",padx = 20, variable=v1, value=1).pack(anchor=W)
Radiobutton(master, text="Negative", padx = 20, variable=v1, value=2).pack(anchor=W)
Radiobutton(master, text="Both", padx = 20, variable=v1, value=3).pack(anchor=W)

Label(master, text="""Choose a tray type:""",justify = LEFT, padx = 20).pack()
a1=Radiobutton(master, text="54",padx=20,variable=v2,value=1).pack(anchor=W)
a2=Radiobutton(master, text="96",padx = 20, variable=v2, value=2).pack(anchor=W)

Label(master, text="""Sort by columns(default) or rows?""",justify = LEFT, padx = 20).pack()
b1=Radiobutton(master, text="columns",padx=20,variable=v3,value=1).pack(anchor=W)
b2=Radiobutton(master, text="rows",padx = 20, variable=v3, value=2).pack(anchor=W)

Label(master, text="""Choose a tray number:""",justify = LEFT, padx = 20).pack()
c1=Radiobutton(master, text="Stk1-01",padx = 20, variable=v4, value=1).pack(anchor=W)
c2=Radiobutton(master, text="Stk1-02", padx = 20, variable=v4, value=2).pack(anchor=W)
c3=Radiobutton(master, text="Stk1-03",padx = 20, variable=v4, value=3).pack(anchor=W)
c4=Radiobutton(master, text="Stk1-04", padx = 20, variable=v4, value=4).pack(anchor=W)
c5=Radiobutton(master, text="MT1-Frnt",padx = 20, variable=v4, value=5).pack(anchor=W)

c6=Radiobutton(master, text="MT1-Rear", padx = 20, variable=v4, value=6).pack(anchor=W)
c7=Radiobutton(master, text="MT2-Frnt",padx = 20, variable=v4, value=7).pack(anchor=W)
c8=Radiobutton(master, text="MT2-Rear", padx = 20, variable=v4, value=8).pack(anchor=W)


Label(master, text="""Would you like to insert a midpoint standard and blank?""",justify = LEFT, padx = 20).pack()
Radiobutton(master, text="Yes",padx = 20, variable=v5, value=1).pack(anchor=W)
Radiobutton(master, text="No", padx = 20, variable=v5, value=2).pack(anchor=W)

Button(master, text="OK", command=ok).pack()

master.mainloop()

我希望有一种方法,如果选择了 a1,则用户无法选择 c6 到 c8。同样地,如果选择了 a1,则用户也无法选择 b1 或 b2。可能有一个使用 sample=DISABLED 使不可选答案变灰或者使用函数让选项在值被选中后出现的方法。感谢任何帮助!
1个回答

0

我在你的示例代码中进行了一些重要但容易的更改,只是为了使事情更加易于访问。

我将所有小部件都包装到一个类中,以便在函数内更好地访问它们。基本上,您想要的只是在a1和a2任何更改时回调函数,如果为1,则禁用c6-8,否则启用它们。

我还不得不使一些pack语句发生在单独的一行上,因为Radiobutton(...).pack()将返回None并使self.someRadiobuttonconfig无法访问。

这里是代码,这样您就可以看到我的意思。

from Tkinter import *

class Window():

    def __init__(self, master):

        self.master = master
        self.v1 = IntVar()
        self.v2 = IntVar()
        self.v3 = IntVar()
        self.v4 = IntVar()
        self.v5 = IntVar()

        Label(master, text="""Which Method do you want to run?""",justify = LEFT, padx = 20).pack()
        Radiobutton(master, text="Positive",padx = 20, variable=self.v1, value=1).pack(anchor=W)
        Radiobutton(master, text="Negative", padx = 20, variable=self.v1, value=2).pack(anchor=W)
        Radiobutton(master, text="Both", padx = 20, variable=self.v1, value=3).pack(anchor=W)

        Label(master, text="""Choose a tray type:""",justify = LEFT, padx = 20).pack()
        self.a1=Radiobutton(master, text="54",padx=20,variable=self.v2,value=1, command = self.disable).pack(anchor=W)
        self.a2=Radiobutton(master, text="96",padx = 20, variable=self.v2, value=2, command = self.disable).pack(anchor=W)

        Label(master, text="""Sort by columns(default) or rows?""",justify = LEFT, padx = 20).pack()
        self.b1=Radiobutton(master, text="columns",padx=20,variable=self.v3,value=1)
        self.b1.pack(anchor=W)
        self.b2=Radiobutton(master, text="rows",padx = 20, variable=self.v3, value=2)
        self.b2.pack(anchor=W)

        Label(master, text="""Choose a tray number:""",justify = LEFT, padx = 20).pack()
        self.c1=Radiobutton(master, text="Stk1-01",padx = 20, variable=self.v4, value=1).pack(anchor=W)
        self.c2=Radiobutton(master, text="Stk1-02", padx = 20, variable=self.v4, value=2).pack(anchor=W)
        self.c3=Radiobutton(master, text="Stk1-03",padx = 20, variable=self.v4, value=3).pack(anchor=W)
        self.c4=Radiobutton(master, text="Stk1-04", padx = 20, variable=self.v4, value=4).pack(anchor=W)
        self.c5=Radiobutton(master, text="MT1-Frnt",padx = 20, variable=self.v4, value=5).pack(anchor=W)

        self.c6=Radiobutton(master, text="MT1-Rear", padx = 20, variable=self.v4, value=6)
        self.c6.pack(anchor=W)
        self.c7=Radiobutton(master, text="MT2-Frnt",padx = 20, variable=self.v4, value=7)
        self.c7.pack(anchor=W)
        self.c8=Radiobutton(master, text="MT2-Rear", padx = 20, variable=self.v4, value=8)
        self.c8.pack(anchor=W)


        Label(master, text="""Would you like to insert a midpoint standard and blank?""",justify = LEFT, padx = 20).pack()
        Radiobutton(master, text="Yes",padx = 20, variable=self.v5, value=1).pack(anchor=W)
        Radiobutton(master, text="No", padx = 20, variable=self.v5, value=2).pack(anchor=W)

        Button(master, text="OK", command=self.ok).pack()

    def ok(self):
        self.master.destroy() 

    def disable(self):

        if self.v2.get() == 1:
            self.b1.config(state = 'disabled')
            self.b2.config(state = 'disabled')
            self.c6.config(state = 'disabled')
            self.c7.config(state = 'disabled')
            self.c8.config(state = 'disabled')

        else:
            self.b1.config(state = 'normal')
            self.b2.config(state = 'normal')
            self.c6.config(state = 'normal')
            self.c7.config(state = 'normal')
            self.c8.config(state = 'normal')

master = Tk()
w = Window(master)
master.mainloop()

如果您不想使用类来完成此操作,您将需要将相关的 Radiobuttons IntVars 引用传递给 disable 函数。如果您想了解如何实现,请在评论中告诉我。

谢谢@maccartm!我得适应类,所以我现在最好开始。 - Jeremy G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接