【问题标题】:Python Tkinter Radiobutton narrowing user inputPython Tkinter Radiobutton缩小用户输入
【发布时间】:2015-09-09 21:25:21
【问题描述】:

我是编程、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 将无法选择的答案变灰,或者使用函数使选项在选择值后出现。任何帮助表示赞赏!

【问题讨论】:

    标签: python button tkinter


    【解决方案1】:

    我在这里对您的示例代码进行了一些重大但简单的更改,只是为了使事情更易于访问。

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

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

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

    【讨论】:

    • 谢谢@maccartm!总有一天我必须适应课程,所以我不妨现在开始。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    相关资源
    最近更新 更多