Corne14
Corne14

Reputation: 41

How can I call back the __init__() method in order to redraw the starting widgets

I am creating a GUI with tkinter and i have a page that has a logIn, when you log in it hides the logIn widgets via .place_forget(), then it draws a button that is supposed to hide the current widgets and then redraw the widgets declared in the init method

I have tried to simply call the init() doing self.init() but i doesn't get the job done

class logIn(ttk.Frame):
    def __init__(self, *args,**kwargs):
        super().__init__(*args,**kwargs)

        self.userName=ttk.Entry(self)
        self.userName.place(x=230,y=50)
        self.userNameLabel=ttk.Label(self,text="User Name")
        self.userNameLabel.place(x=120, y =50)

        self.password= ttk.Entry(self)
        self.password.place(x=230,y=100)
        self.passwordLabel = ttk.Label(self, text="Password")
        self.passwordLabel.place(x=120, y=100)

        self.logIn=ttk.Button(self,text="Log In",command=self.getLoginInfo)
        self.logIn.place(x=200,y=200)

    def getLoginInfo(self):
        userName=self.userName.get()
        password=self.password.get()


        logInList=[['1','a','0'],['2','b','1'],['3','c','0']]


        login_success=False
        migration_status=False

        for user in logInList:
            if userName == user[0]:
                if password==user[1] and user[2]!='1':
                    login_success=True
                    migration_status=False
                elif password==user[1] and user[2]=='1':
                    messagebox.showinfo("Access denied","User with migration issues")
                    login_success=False
                    migration_status=True
        if not login_success and not migration_status:
            messagebox.showinfo("Access denied", "Wrong User Name or password")
        elif login_success and not migration_status:
            for child in self.winfo_children():
                child.place_forget()
            self.draw_recervation()

        self.userName.delete(0, tk.END)
        self.password.delete(0, tk.END)

    def draw_recervation(self):

        self.searchKey=[]

        self.countryList = ttk.Combobox(self, state="readonly")

        self.countryList["values"] = ["1", "2", "3"]

        self.countryList.bind("<<ComboboxSelected>>", self.updateCitiesOnSelection)
        self.countryList.place(x=250, y=50)
        self.countryListLabel=ttk.Label(self,text="Countries")
        self.countryListLabel.place(x=100,y=50)

        self.cityList = ttk.Combobox(self, state="readonly")
        self.cityList.bind("<<ComboboxSelected>>", self.selectCityAndUpdateRoutes)
        self.cityList.place(x=250, y=100)
        self.cityListLabel=ttk.Label(self,text="Cities")
        self.cityListLabel.place(x=100, y = 100)

        self.routeList = ttk.Combobox(self, state="readonly")
        self.routeList.bind("<<ComboboxSelected>>", self.getRoute)
        self.routeList.place(x=250, y=150)
        self.routeListLabel=ttk.Label(self, text="Routes")
        self.routeListLabel.place(x=100,y=150)

        self.backButton=ttk.Button(self, text="back", command=self.backToLogIn)
        self.backButton.pack()

        def backToLogIn(self):
            for child in self.winfo_children():
                child.place_forget()

            #this is the method call I tried
            self.__init__()

Upvotes: 0

Views: 140

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386240

How can I call back the init() method in order to redraw the starting widgets

You don't want to run __init__ a second time, unless you destroy and recreate the object. __init__ is designed to be called exactly once when the object is instantiated.

Instead, create a function with the code you want to run a second time, and then call that function from __init__ and then also call it when you want the code to run a second time.

class logIn(ttk.Frame):
    def __init__(self, *args,**kwargs):
        super().__init__(*args,**kwargs)

        self.initialize_gui()

    def initialize_gui(self):

        # delete any existing widgets
        for child in self.winfo_children():
            child.destroy()

        # create the widgets

        self.userName=ttk.Entry(self)
        self.userName.place(x=230,y=50)
        self.userNameLabel=ttk.Label(self,text="User Name")
        self.userNameLabel.place(x=120, y =50)

        self.password= ttk.Entry(self)
        self.password.place(x=230,y=100)
        self.passwordLabel = ttk.Label(self, text="Password")
        self.passwordLabel.place(x=120, y=100)

        self.logIn=ttk.Button(self,text="Log In",command=self.getLoginInfo)
        self.logIn.place(x=200,y=200)

Upvotes: 1

Related Questions