Home > database >  TypeError in __init__() unable to solve
TypeError in __init__() unable to solve

Time:10-17

from tkinter import *
from tkinter.ttk import *


class window(Tk):
    def __init__(self, **kwargs):
        Tk.__init__(self, **kwargs)

        box=Frame(self).pack(fill=BOTH,expand=1)
        login=Frame(box,self).pack(fill=BOTH,expand=1)
        register=Frame(box,self).pack(fill=BOTH,expand=1)

        self.fup(login)

    def fup(self,f):
        f.tkraise()

class login(Frame):
    def __init__(self, parent, controller,**kwargs):
        Frame.__init__(self, parent)
        super().__init__(**kwargs)

        lab=Label(self,text='Login').grid(row=0,column=3,pady=3)
        but=Button(self,text='Signup',command=lambda : controller.fup(register)).grid(row=4,column=0)

class register(Frame):
    def __init__(self, parent, controller,**kwargs):
        Frame.__init__(self, parent)
        super().__init__(**kwargs)

        lab=Label(self,text='Register').grid(row=0,column=3,pady=3)
        but=Button(self,text='<<',command=lambda : controller.fup(login)).grid(row=0,column=0)



win=window()
win.mainloop()

CodePudding user response:

You have a number of problems here. First, you're confused about how classes and class objects work. When you said

        login=Frame(box,self).pack(fill=BOTH,expand=1)

That does not create an object of class login. It creates a simple Frame. In this case, it doesn't store an object at all, because the pack method does not return anything. In your login class, you refer to register, but that isn't an object, it's a class.

This is closer to what you want, and brings up a window, but since I can't tell what you really wanted, you'll have to take it from here.

from tkinter import *
from tkinter.ttk import *

class login(Frame):
    def __init__(self, parent, controller,**kwargs):
        super().__init__(parent, **kwargs)

        lab=Label(self,text='Login').grid(row=0,column=3,pady=3)
        but=Button(self,text='Signup',command=lambda : controller.fup(controller.register)).grid(row=4,column=0)

class register(Frame):
    def __init__(self, parent, controller,**kwargs):
        super().__init__(parent, **kwargs)

        lab=Label(self,text='Register').grid(row=0,column=3,pady=3)
        but=Button(self,text='<<',command=lambda : controller.fup(controller.login)).grid(row=0,column=0)


class window(Tk):
    def __init__(self, **kwargs):
        Tk.__init__(self, **kwargs)

        box=Frame(self) 
        box.pack(fill=BOTH,expand=1)
        self.login=login(box,self) 
        self.login.pack(fill=BOTH,expand=1)
        self.register=register(box,self) 
        self.register.pack(fill=BOTH,expand=1)

        self.fup(self.login)

    def fup(self,f):
        f.tkraise()

win=window()
win.mainloop()
  • Related