我如何回调__init __()方法以重绘开始的小部件

时间:2019-05-22 02:14:11

标签: python python-3.x tkinter

我正在用tkinter创建一个GUI,并且我有一个具有登录页面,当您登录时,它会通过.place_forget()隐藏登录窗口小部件,然后绘制一个应该隐藏当前窗口小部件的按钮,然后重绘在 init 方法

中声明的小部件

我试图简单地称呼 init ()做自我。 init ()但我没有完成工作

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

1 个答案:

答案 0 :(得分:1)

  

如何重新调用 init ()方法以重绘开始的小部件

除非您销毁并重新创建对象,否则您不想第二次运行__init____init__设计为在实例化对象时仅被调用一次。

相反,使用您想第二次运行的代码创建一个函数,然后从__init__调用该函数,然后在您希望第二次运行的代码时也调用它。

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)