我的gui应用程序不能同时工作

时间:2017-09-18 14:19:43

标签: python tkinter

该代码适用于具有登录页面的gui应用程序

from tkinter import *
import tkinter.messagebox as tm
import restaurant_management_system


class LoginFrame(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.label_1 = Label(self, text="Username")
        self.label_2 = Label(self, text="Password")

        self.entry_1 = Entry(self)
        self.entry_2 = Entry(self, show="*")

        self.label_1.grid(row=0, sticky=E)
        self.label_2.grid(row=1, sticky=E)
        self.entry_1.grid(row=0, column=1)
        self.entry_2.grid(row=1, column=1)

        self.checkbox = Checkbutton(self, text="Keep me logged in")
        self.checkbox.grid(columnspan=2)

        self.logbtn = Button(self, text="Login", command = self._login_btn_clickked)
        self.logbtn.grid(columnspan=2)

        self.pack()

    def _login_btn_clickked(self):
        #print("Clicked")
        username = self.entry_1.get()
        password = self.entry_2.get()

        #print(username, password)

        if username == "john" and password == "password":
            tm.showinfo("Login info", "Welcome John")
        else:
            tm.showerror("Login error", "Incorrect username")

root = Tk()
lf = LoginFrame(root)
root.mainloop()

我导入了另一个py文件,但是当用户成功登录时我不知道如何运行它。

1 个答案:

答案 0 :(得分:0)

您基本上需要检查用户名和密码是否正确,然后销毁包含小部件的frame并在另一侧重绘您想要的内容:

from tkinter import *

class App:
    def __init__(self, root):
        self.username = "Username"
        self.password = "Password"

        self.root = root
        self.frame = Frame(self.root)
        self.entry1 = Entry(self.frame)
        self.entry2 = Entry(self.frame)
        self.button1 = Button(self.frame, text="Ok", command=self.login)
        self.frame.pack()
        self.entry1.pack()
        self.entry2.pack()
        self.button1.pack()
    def login(self):
        if self.entry1.get() == self.username and self.entry2.get() == self.password:
            self.frame.destroy()
            self.frame = Frame(self.root)
            self.label = Label(self.frame, text="You are logged in")
            self.frame.pack()
            self.label.pack()

root = Tk()
App(root)
root.mainloop()