save class instances in lists as variables(outside of a class) Python -
this question has answer here:
class questions(tkinter.tk): def __init__(self, *args, **kwargs): tkinter.tk__init__(self, *args, **kwargs) container = tkinter.frame(self) container.pack(side='top', fill='both', expand=true) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} f in (startpage, questionone, questiontwo) frame = f(container, self) self.frames[f] = frame frame.grid(row = 0, column= 0, sticky='nsew') self.show_frame(startpage) def show_frame(self, cont): frame = self.frames[cont] frame.tkraise() def show_choice(self, cont) global pr_var global x questions.pr_var = x.get() var_list[0]= questions.pr_var print('pr_var = ' + str(questions.pr_var)) ## print check frame= self.frames[questionone] frame.tkraise() class startpage(tkinter.frame): def __init__(self, parent, controller): tkinter.frame.__init__(self, parent) label= tkinter.label(self, text='hello', font= large_font) label.pack(pady=10, padx=10) button=ttk.button(self, text='start', command= lambda:controller.show_frame(questionone)) button.pack() class questionone(tk.frame): def __init__(self, parent, controller): tkinter.frame.__init__(self, parent) label= tkinter.label(self, text='how many pieces?', font= large_font) label.grid(row= 3, column=1, pady=10, padx=50) global x self.radio_num= tkinter.intvar() x = self.radio_num self.rb1 = tkinter.radiobutton(self, text='one', variable=self.radio_num, value=1) self.rb2 = tkinter.radiobutton(self, text='two', variable=self.radio_num, value=2) self.rb1.grid(row=5, column=0, padx=10) self.rb2.grid(row=6, column=0, padx=10) button1=ttk.button(self, text='next', command= lambda:controller.show_choice(self)) button2=ttk.button(self, text='back', command= lambda:controller.show_frame(startpage)) button1.grid(row=25, column=1, padx=10, pady=50) button2.grid(row=25, column=0, padx=10, pady=50) class questiontwo(tkinter.frame): def __init__(self, parent, controller): tkinter.frame.__init__(self, parent) label= tkinter.label(self, text='so have ' +str(var_list[0]) + ' piece(s)', font= large_font) ## here problem label.grid(row= 3, column=1, pady=10, padx=50) global x global pr_var button1=ttk.button(self, text='back', command= lambda:controller.show_frame(questionone)) button1.grid(row=25, column=1, padx=10, pady=50) app=questions() app.mainloop()
i working on program , appreciate bit of help. pretty new in programming in general please patient.. :-)
so wrote program takes variables user , processes them(i completed processing program here there no needed). started writing gui questions asked , input user saved variables. @ end of these questions variables saved , used processing program.
so stuck cannot save variables!!
i using classes gui..where first class holds main frame in form of:
def __init__(self, *args, **kwargs)
and every page use, create new class(new frame) refers primary class.
now problem example @ page 2 user chooses first time radio button value calculated method of primary class(where every new page refers) 'command' , inside method print value after calculation (in order see if saved correctly) , works. until here ok.
my problem when use saved variable in page 3 doesnt work...
i tried make class instance global variable , works inside method of primary class..after when want use in class(i mean in page) doesn't work. tried make reference classa.variable didn't work either.. try define variable inside init method none(otherwise ''variable'' not defined) , when being calculated page 2 works when in page 3 want put variable in form of string inside label tkinter value none although supposed global variable...i confused
my main goal make list variables user going give radio-buttons. list must outside of list in order processing program going access them easily.
i tried too..to make list when refer list[0] ( example first variable that... list index out of range
how can take class instances , turn them list components variables??
ps: variables not integers or strings..the references class instance example......
list[0] = x.get()
where global x ,
self.radio_num =tkinter. intvar()
and
x= self.radio_num
ps2: dont know if explained correctly can understand problem is...if not, upload code if needed
either way anyway :-)
create list named classes @ start of module. in __init__()
function of classes want store, put:
global classes classes.append(self)
this allow access variables attributes.
Comments
Post a Comment