Python Tkinter Radiobutton 缩小用户输入范围

Python Tkinter Radiobutton narrowing user input

我是编程新手,python,我和 Tkinter 想要一个很好的解决方案(也许使用 state=DISABLED?)来根据他们 select 的按钮限制用户的选项。 我现在的代码:

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 被 selected,则 c6 到 c8 不能被用户 selected。同样,如果 a1 是 selected,则用户将无法 select b1 或 b2。可能有一种方法可以使用 sample=DISABLED 使无法select 的答案变灰,或者可以使用一个函数使选项在值被 selected 后出现。感谢您的帮助!

我在这里对您的示例代码做了一些重要但简单的更改,只是为了让事情更容易访问。

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

我还必须让你的一些包语句在单独的行上发生,因为 Radiobutton(...).pack() 将 return 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()

如果您想在不使用 class 的情况下执行此操作,则必须将对相关 RadiobuttonsIntVars 的引用传递给 disable 函数。如果您想了解如何在评论中告诉我。