Getting value from radiobox in Tkinter - Python -
i'm trying create gui in python using tkinter module, , part of involves giving user option of 2 radioboxes, , select 1 want. depending on box tick, runs different functions return different results - results want use outside of window class. don't know how send value inside class outside class; i'm sure simple can't life of me work out.
my current code is:
class batchindiv(): def __init__(self, master): self.master=master self.startwindow() self.b=0 def startwindow(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) def buttonclick(self): if (self.var1.get())==1: b=batchimport() return b self.master.quit() self.master.destroy() elif (self.var1.get())==2: b=indivimport() return b self.master.quit() self.master.destroy() else: pass def cb1select(self): return self.var1.get() #end of class definition. #code: root=tk() window=batchindiv(root) b=batchindiv.buttonclick.b root.mainloop() ....
treat batchimport , indivimport functions black boxes, return integer value, assign variable b inside buttonclick(). need value stuff below root.mainloop(), (i.e. .... is), don't know how it. tkinter quite irritating, has different methods of doing things online documentation never same - tried doing written in various ones , gave me more lovely error messages.
any , appreciated.
ps - how can make window close when button pressed, , still send value b rest of code, , not quit python completely? can see tried using .quit() , .destroy() no luck.
your variable b
local class, moment class deleted (after destroy
or quit
), b
gets destroyed. define variable b
global.
b = 0 # in global namespace class batchindiv(): def __init__(self, master): self.master=master self.startwindow() #self.b=0 # no need this, directly store in global variable def startwindow(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) def buttonclick(self): global b if (self.var1.get())==1: b=batchimport() self.master.quit() #self.master.destroy() # either quit or destroy, think 1 sufficient, confirm sure. elif (self.var1.get())==2: b=indivimport() self.master.quit() #self.master.destroy() # either quit or destroy, think 1 sufficient, confirm sure else: pass def cb1select(self): return self.var1.get() #end of class definition. #code: root=tk() window=batchindiv(root) root.mainloop() # here whatever want variable b print b
(using global variable not idea, since don't know want b
, not able suggest anything.)
Comments
Post a Comment