Python Tkinter Radiobutton narrows user input

I'm new to programming, python and Tkinter and I need a good solution (perhaps using state = DISABLED?) To restrict user options based on the buttons they have selected. My real code:

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()

      

I need a way so that if a1 is selected, c6-c8 cannot be selected by the user. Likewise, if a1 is selected, the user will not be able to select either b1 or b2. There is probably an option to use sample = DISABLED to gray out the unselected answers, or maybe use a function to have the options appear after a value is selected. Any help is appreciated!

+3


source to share


1 answer


I've made some significant, albeit simple, changes to your sample code here to make things more accessible.

I have included all the widgets in the class so I can better access them within functions. Basically, you just want a callback function on any change to a1 and a2 that will disable c6-8 if 1, then enable them.

I also had to do some of your package assertions on a separate line as it Radiobutton(...).pack()

will return None

and make it self.someRadiobutton

config

unavailable.



Here is the code so you can see what I mean.

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()

      

If you want to do this without using a class, you need to pass a reference to the relevant ones Radiobuttons

and IntVars

to the function disable

. If you want to see how simple let me know in the comment.

0


source







All Articles