0

I want the Pin variable which takes some input in class StartPage to be inherited by the class Options, but using any code that uses Pin variable for eg. print(Pin) gives an error NameError: name 'Pin' is not defined. Passing both StartPage and tk.Frame as parent to class Option generates the error TypeError Cannot create a consistent method resolution order (MRO) for bases Frame, StartPage. Don't Judge me, as I am new to both Tkinter and OOP, any explained solution is highly appreciated.

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Enter 4 digit pin", font=controller.title_font, fg="red")
        label.pack(side="top", fill="x", pady=10)

        Pin = tk.Entry(self, bd = 4, relief="groove", show="*", font=20, justify='center')
        Pin.pack()

        submit = tk.Button(self, text="Submit", width=12,font=tkfont.Font(size=12),command=lambda: controller.show_frame("Options"), cursor="hand2")
        submit.pack()



class Options(tk.Frame,StartPage):

    def __init__(self, parent, controller):
        print(Pin)
        tk.Frame.__init__(self, parent)
        self.controller = controller
        f1=tk.Frame(self)
        f1.pack()
        f2=tk.Frame(self)
        f2.pack(side="bottom")
        button1 = tk.Button(f1, text="Balance Inquiry",relief="flat", padx=30, justify='left', cursor="hand2", fg="red", font=tkfont.Font(size=10),
                           command=lambda: controller.show_frame("PageOne"))
        button1.pack(side="left")

        button2 = tk.Button(f1, text="Deposit",relief="flat", padx=50, justify='right', cursor="hand2", fg="red", font=tkfont.Font(size=10),
                           command=lambda: controller.show_frame("PageTwo"))
        button2.pack(side="left")

        button3 = tk.Button(f2, text="Withdraw",relief="flat", padx=50, justify='left', cursor="hand2", fg="red", font=tkfont.Font(size=10),
                           command=lambda: controller.show_frame("PageThree"))
        button3.pack(side="left")

        button4 = tk.Button(f2, text="Pin Change",relief="flat", padx=50, justify='right', cursor="hand2", fg="red", font=tkfont.Font(size=10),
                           command=lambda: controller.show_frame("PageFour"))
        button4.pack(side="left")
Jack Duane
  • 185
  • 3
  • 17
H4rd_C0d3
  • 5
  • 4

1 Answers1

1

I want the Pin variable which takes some input in class StartPage to be inherited by the class Options.

Use self.Pin. Also there is convention for instance names to start with lowercase, so self.pin. Initialisation should also be with self:

class StartPage(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.pin = tk.Entry(self, bd = 4, relief="groove", show="*", font=20, justify='center')
    self.pin.pack()
    ...

and always put your parent's constructor as first statement in child's __init__.

class Options(StartPage):
    def __init__(self, parent, controller): 
        StartPage.__init__(self, parent)
        print(self.pin)

What should you do?

Composition over inheritance. If you want to have just one field from StartPage in your Options class you can simply pass pin as argument.

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.pin = tk.Entry(self, bd = 4, relief="groove", show="*", font=20, justify='center')
        self.pin.pack()
        ...


class Options(fk.Frame):

    def __init__(self, parent, controller, pin):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        print(pin)
        ...

And then from outside it will look like this:

start_page = StartPage(some_parent, some_controller)
options = Options(some_parent, some_controller, start_page.pin)
Dawid Fieluba
  • 1,271
  • 14
  • 34