-1
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 = {}

        for 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)) ## i print it to 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 you have ' +str(var_list[0]) + ' piece(s)', font= LARGE_FONT)  ## here is the 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 am currently working on a program and i would appreciate a bit of help. i am pretty new in programming in general so please be Patient.. :-)

so i wrote a program that takes some variables from the user and then processes them(i completed this processing program so here there is no help needed). Then i started writing a GUI where some questions will be asked and the Input from the user will be saved as variables. at the end of These Questions all the variables will be saved and used from the processing program.

So where i am stuck is that i cannot save the variables!!

i am using classes for the Gui..where the first class holds the main Frame in form of:

def __init__(self, *args, **kwargs) 

and for every page i use, i create a new class(new Frame) that refers to the primary class.

Now the problem is that for example at page 2 where the user chooses for the first time a radio button then the value is calculated from a method of the Primary class(where every new page always refers) with 'command' and inside this method i print the value after the calculation (in order to see if it is saved correctly) and it works. Until here is everything ok.

My problem is when i use the saved variable in page 3 it doesnt work...

i tried to make the class instance as global variable and it works only inside the method of the Primary class..after when i want to use it in an another class(i mean in another page) it doesn't work. i tried to make a reference as ClassA.variable but that didn't work either.. i try to define the variable inside the init method with None(otherwise i get that the ''variable'' is not defined) and when its being calculated from page 2 it works but when in page 3 i want to put the variable in form of a string inside a Label with tkinter then i get the value None although i was supposed to be a global variable...i am very confused

My main Goal is to make a list with the variables that the user is going to give with radio-buttons. This list must be outside of a list in order the processing program is going to Access them easily.

i tried that too..to make a list but when i refer to the list[0] ( for example for the first variable then i get that... list index out of range

How can i take class instances and turn them into list components as variables??

PS: the variables are not integers or strings..the are references to a class instance for example......

list[0] = x.get()

where global x and

self.radio_num =tkinter. IntVar()

and

x= self.radio_num

PS2: i dont know if i explained everything correctly so that somebody can understand exactly what my Problem is...if not, i could upload some code if needed

either way thanks anyway :-)

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
GeorgM
  • 17
  • 6

1 Answers1

-1

Create a list named classes at the start of your module. In the __init__() function of the classes you want to store, put:

global classes
classes.append(self)

This will allow you to access the variables as attributes.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • can you please explain me how to do it?? i tried making the list global inside the method Show_choice but it still doesnt work on page 2 – GeorgM Sep 12 '17 at 11:48
  • I clarified my answer to explain how you would implement the list. – Temsia Carrolla Sep 12 '17 at 12:32
  • thanks for your help but i still cannot make it work..first of all if i am not mistaken whenever somebody uses append it is just going to add Content to the list and if the user pushes back and then next maybe the variables are not going to be in the right place afterwards..so i think the insert method would be safer that the variables are always saved in the correct index. and second of all i typed it exactly as you told me and the good News is that i no longer see None when it uses the variable from the list but it gives me number like .58345200.72500432 where the correct answer would be '1' – GeorgM Sep 12 '17 at 14:00
  • i am sure that for you it is pretty simple..it sounds also to my ears simple(to save the variables in a list) but unfortunately it still doesnt work...thanks for your Patience :-) – GeorgM Sep 12 '17 at 14:04