Python Tkinter Radiobutton output

I'm trying to find a way to take a value from a Radiobutton, set it to a variable, and use the variable outside of the class in other parts of my code. Here's an example of my code:

from Tkinter import *

class Window():

    def __init__(self, master):

        self.master = master
        self.v1 = IntVar()

        Label(master, text="""Which Method?""",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)

      

Is there a way, preferably without using the definitions and command options, to set the Method variable to a value of the user's choice? And then use that value outside of the class.

I tried using Method = self.v1.get (), but it didn't work.

+3


source to share


1 answer


from Tkinter import *

class Window():

    def __init__(self, master):

        self.master = master
        self.v1 = IntVar()

        Label(master, text="""Which Method?""",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)

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

print "The radiobutton outside of the class: %d" %w.v1.get()

      

Here's an example of what I mean in my comment. My example is to print value

what is selected Radiobutton

when the window is closed.



In your case, depending on which value matters Radiobutton

, you have to decide which functions to call.

+2


source







All Articles