0

I tried to create a multi-frame python application. However, when I try to retrieve the information from the radiobutton, python throws me an error saying the variable "movie " is not defined. Can someone explaining me what am I missing?

To my understanding, I guess the problem is that the variable is declared inside the class. I tried to make the variable global, but it did not help in solving the problem.

import tkinter as tk
from tkinter import *
from tkinter import ttk

LARGE_FONT=("Verdana", 12)

class Movies(tk.Tk):

    
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self,*args, **kwargs)

        tk.Tk.wm_title(self, "Movie I like")
        tk.Tk.wm_geometry(self, "500x500")
        container=tk.Frame(self)
        container.pack(side="top", fill="both", expand="True")
        
        self.frames={}

        for F in (StartPage, PageOne):

            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()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        s = ttk.Style()
        s.configure('my.TButton', font=('Verdana', 8))

        label=tk.Label(self, text="Home Page", font=LARGE_FONT)
        label.pack(pady=10, padx=30)

        button_load=ttk.Button(self, text="Kind of Movie ", width=30, style='my.TButton',
                          command= lambda: controller.show_frame(PageOne))
        button_load.pack(padx=30)

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
                
        movie= tk.StringVar()
        movie.set("1")
        mybutton=ttk.Radiobutton(self, text='Drama', variable=movie, value="Drama").grid(column=1,row=1, sticky=W, padx=10)
        mybutton1=ttk.Radiobutton(self, text='Comedy', variable=movie, value="Comedy").grid(column=2,row=1, sticky=W, padx=10)
        
        button_submit=ttk.Button(self, text="Submit", width=15,
                          command=save_info).grid(column=3, row= 10, padx=5)

def save_info():
    movie_info=movie.get()
    print("you like " + movie_info)



app=Movies()
app.mainloop()
  • your `movie` variable is local. It needs to be `self.movie` – Bryan Oakley Aug 09 '21 at 12:51
  • When I replace movie with self.movie it tells me that self is not defined..not sure what am I doing wrong – startingover Aug 09 '21 at 12:56
  • That is because `save_info` is not a class method. It needs to be a class method. – Bryan Oakley Aug 09 '21 at 12:58
  • @BryanOakley shouldn't it be an instance method (because `movie` (which should be `self.movie` anyways) is defined in the `__init__` method)? also then need to add `self.` to all the `movie` var names in the `__init__` method and pass the `self` argument to that method too – Matiiss Aug 09 '21 at 13:14
  • Thanks, now it works! Just a question, If I wanted to create another class (frame) in order to collect information on different screens, could I print at the end all the information collected, or is it the variable available just for the specific class? – startingover Aug 09 '21 at 13:22
  • See https://stackoverflow.com/q/32212408/7432 and https://stackoverflow.com/questions/33646605/how-to-access-variables-from-different-classes-in-tkinter – Bryan Oakley Aug 09 '21 at 13:47

0 Answers0