Getting Value From Radiobox In Tkinter - Python
Solution 1:
Your variable b
is just local to your class, so it the moment your class is deleted (after you do destroy
or quit
), b
gets destroyed. So define the variable b
as global.
b = 0# this is now in the global namespaceclassBatchIndiv():
def__init__(self, master):
self.master=master
self.startwindow()
#self.b=0 # no need for this, directly store in the global variabledefstartwindow(self):
self.var1 = IntVar()
self.textvar = StringVar()
self.Label1=Label(self.master, text="Batch or indivdual import?")
self.Label1.grid(row=0, column=0)
self.Label2=Label(self.master, textvariable=self.textvar)
self.Label2.grid(row=2, column=0)
self.rb1 = Radiobutton(self.master, text="Batch", variable=self.var1,
value=1, command=self.cb1select)
self.rb1.grid(row=1, column=0, sticky=W)
self.rb2 = Radiobutton(self.master, text="Individual", variable=self.var1,
value=2, command=self.cb1select)
self.rb2.grid(row=1, column=1, sticky=W)
self.Button1=Button(self.master, text="ok", command=self.ButtonClick)
self.Button1.grid(row=1, column=2)
defButtonClick(self):
global b
if (self.var1.get())==1:
b=BatchImport()
self.master.quit()
#self.master.destroy() # either quit or destroy, I think one is sufficient, but confirm to be sure.elif (self.var1.get())==2:
b=IndivImport()
self.master.quit()
#self.master.destroy() # either quit or destroy, I think one is sufficient, but confirm to be sureelse: passdefcb1select(self):
return self.var1.get()
#End of class definition.#Code:
root=Tk()
window=BatchIndiv(root)
root.mainloop()
# now do here whatever you want to do with the variable bprint b
(using global variable is not a good idea, but since I don't know what you want to do with b
, I am not able to suggest anything.)
Solution 2:
Generally speaking you should never have any code after the call to mainloop
, that's just not how GUI programs work.
When the user presses the button, all the work should happen (or be started from) the command for the button. If you then want the program to exit you destroy the root window (which causes the mainloop
method to exit), and your program ends. You shouldn't be running things after mainloop.
... and because of that, the question of how to pass data from the buttons to the code after mainloop
becomes moot.
So, create a method that does whatever ...
in your example does. Call that method from within ButtonClick
, and when you call it you can pass in any information from the GUI that you want.
ButtonClick
then becomes something like this:
defButtonClick(self):
if self.var1.get()==1:
b=BatchImport()
elif self.var1.get()==2:
b=IndivImport()
self.DotDotDot(b)
self.master.quit()
Post a Comment for "Getting Value From Radiobox In Tkinter - Python"