Tkinter密码检查器(切换帧))

时间:2017-10-04 09:56:05

标签: python tkinter

我非常难以尝试为我的tkinter输入密码进行验证。我想要发生的是程序在用户按下“登录”按钮时检查密码。我希望将密码存储在可以读取的文本文件中。我知道如何实现文件的读取,但是如果接受密码,我无法弄清楚如何切换帧。提前谢谢

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

LARGE_FONT = ("Verdana", 12)

Background = ('#e6eeff')

class roomBooker(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (loginPage, signupPage, mainMenu):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(loginPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.configure(background='#e6eeff')
        frame.tkraise()


def show_frame(self, cont):
    frame = self.frames[cont]
    frame.configure(background='#e6eeff')
    frame.tkraise()

class loginPage(tk.Frame):
    def __init__(self, master, controller):
        tk.Frame.__init__(self,master)

        self.title_1 = tk.Label(self, text="Room Booker 1.1",bg=Background ,font=("Verdana", 18))
        self.title_1.place(x=500, y=150)

        self.label_1 = tk.Label(self, text="Username:",bg=Background ,font=("Verdana", 12))
        self.label_1.place(x=500, y=200)

        self.entry_1 = tk.Entry(self, bg='#abb5c6',font=("Verdana", 10))
        self.entry_1.place(x=600, y=203)

        self.label_2 = tk.Label(self, text="Password:",bg=Background ,font=("Verdana", 12))
        self.label_2.place(x=500, y=250)

        self.entry_2 = tk.Entry(self, show="*", bg='#abb5c6',font=("Verdana", 10))
        self.entry_2.place(x=600, y=253)

        self.logbtn = tk.Button(self, text="Login", command=self._password_check, font=("Verdana", 10), width=14, relief=GROOVE)
        self.logbtn.place(x=600, y=293)

        self.button = tk.Button(self, text="Signup Now",command=lambda: controller.show_frame(signupPage), font=("Verdana", 10), width=14, relief=GROOVE)
        self.button.place(x=600, y=333)


    def _password_check(self):
        print("Clicked")

        username = self.entry_1.get()
        password = self.entry_2.get()
        print(username, password)

        if username == "test" and password == "test":
            tm.showinfo("Login info", "Welcome Test")
            show_frame(mainMenu)
        else:
            tm.showerror("Login error", "Incorrect username or password")


class signupPage(tk.Frame):

    def __init__(self, master, controller):
        tk.Frame.__init__(self, master)

##        button1 = tk.Button(self, text="Back to Home",
##                            command=lambda: controller.show_frame(loginPage))
##        button1.pack()

        self.title_1 = tk.Label(self, text="Signup",bg=Background ,font=("Verdana", 18))
        self.title_1.place(x=500, y=150)

        self.title_1 = tk.Label(self, text="Please enter a new username and password",bg=Background ,font=("Verdana", 8))
        self.title_1.place(x=500, y=185)

        self.label_1 = tk.Label(self, text="Email:",bg=Background ,font=("Verdana", 12))
        self.label_1.place(x=500, y=200)

        self.entry_1 = tk.Entry(self, bg='#abb5c6',font=("Verdana", 10))
        self.entry_1.place(x=600, y=203)

        self.label_2 = tk.Label(self, text="Username:",bg=Background ,font=("Verdana", 12))
        self.label_2.place(x=500, y=250)

        self.entry_2 = tk.Entry(self, show="*", bg='#abb5c6',font=("Verdana", 10))
        self.entry_2.place(x=600, y=253)

        self.label_3 = tk.Label(self, text="Password:",bg=Background ,font=("Verdana", 12))
        self.label_3.place(x=500, y=300)

        self.entry_3 = tk.Entry(self, bg='#abb5c6',font=("Verdana", 10))
        self.entry_3.place(x=600, y=303)

        self.logbtn = tk.Button(self, text="Signup",command=lambda: controller.show_frame(mainMenu), font=("Verdana", 10), width=14, relief=GROOVE)
        self.logbtn.place(x=600, y=343)

        self.button = tk.Button(self, text="Return to Login",command=lambda: controller.show_frame(loginPage), font=("Verdana", 10), width=14, relief=GROOVE)
        self.button.place(x=600, y=383)    


class mainMenu(tk.Frame):
    def __init__(self, master, controller):
        tk.Frame.__init__(self, master)

        label = tk.Label(self, text="Main Menu",bg=Background ,font=("Verdana", 18))
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(loginPage))
        button1.pack()


app = roomBooker()
app.geometry('1280x720')
app.configure(background='#e6eeff')
app.title('Room Booker')
app.wm_iconbitmap("applicationlogo.ico")
app.mainloop()

1 个答案:

答案 0 :(得分:1)

show_frame下的_password_check来电不正确。试试这个:

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

LARGE_FONT = ("Verdana", 12)

Background = ('#e6eeff')

class roomBooker(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (loginPage, signupPage, mainMenu):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(loginPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.configure(background='#e6eeff')
        frame.tkraise()

class loginPage(tk.Frame):
    def __init__(self, master, controller):
        tk.Frame.__init__(self,master)

        self.title_1 = tk.Label(self, text="Room Booker 1.1",bg=Background ,font=("Verdana", 18))
        self.title_1.place(x=500, y=150)

        self.label_1 = tk.Label(self, text="Username:",bg=Background ,font=("Verdana", 12))
        self.label_1.place(x=500, y=200)

        self.entry_1 = tk.Entry(self, bg='#abb5c6',font=("Verdana", 10))
        self.entry_1.place(x=600, y=203)

        self.label_2 = tk.Label(self, text="Password:",bg=Background ,font=("Verdana", 12))
        self.label_2.place(x=500, y=250)

        self.entry_2 = tk.Entry(self, show="*", bg='#abb5c6',font=("Verdana", 10))
        self.entry_2.place(x=600, y=253)

        self.logbtn = tk.Button(self, text="Login", command=self._password_check, font=("Verdana", 10), width=14, relief=GROOVE)
        self.logbtn.place(x=600, y=293)

        self.button = tk.Button(self, text="Signup Now",command=lambda: controller.show_frame(signupPage), font=("Verdana", 10), width=14, relief=GROOVE)
        self.button.place(x=600, y=333)


    def _password_check(self):
        print("Clicked")

        username = self.entry_1.get()
        password = self.entry_2.get()
        print(username, password)

        if username == "test" and password == "test":
            tm.showinfo("Login info", "Welcome Test")
            app.show_frame(mainMenu)
        else:
            tm.showerror("Login error", "Incorrect username or password")


class signupPage(tk.Frame):

    def __init__(self, master, controller):
        tk.Frame.__init__(self, master)

##        button1 = tk.Button(self, text="Back to Home",
##                            command=lambda: controller.show_frame(loginPage))
##        button1.pack()

        self.title_1 = tk.Label(self, text="Signup",bg=Background ,font=("Verdana", 18))
        self.title_1.place(x=500, y=150)

        self.title_1 = tk.Label(self, text="Please enter a new username and password",bg=Background ,font=("Verdana", 8))
        self.title_1.place(x=500, y=185)

        self.label_1 = tk.Label(self, text="Email:",bg=Background ,font=("Verdana", 12))
        self.label_1.place(x=500, y=200)

        self.entry_1 = tk.Entry(self, bg='#abb5c6',font=("Verdana", 10))
        self.entry_1.place(x=600, y=203)

        self.label_2 = tk.Label(self, text="Username:",bg=Background ,font=("Verdana", 12))
        self.label_2.place(x=500, y=250)

        self.entry_2 = tk.Entry(self, show="*", bg='#abb5c6',font=("Verdana", 10))
        self.entry_2.place(x=600, y=253)

        self.label_3 = tk.Label(self, text="Password:",bg=Background ,font=("Verdana", 12))
        self.label_3.place(x=500, y=300)

        self.entry_3 = tk.Entry(self, bg='#abb5c6',font=("Verdana", 10))
        self.entry_3.place(x=600, y=303)

        self.logbtn = tk.Button(self, text="Signup",command=lambda: controller.show_frame(mainMenu), font=("Verdana", 10), width=14, relief=GROOVE)
        self.logbtn.place(x=600, y=343)

        self.button = tk.Button(self, text="Return to Login",command=lambda: controller.show_frame(loginPage), font=("Verdana", 10), width=14, relief=GROOVE)
        self.button.place(x=600, y=383)    


class mainMenu(tk.Frame):
    def __init__(self, master, controller):
        tk.Frame.__init__(self, master)

        label = tk.Label(self, text="Main Menu",bg=Background ,font=("Verdana", 18))
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(loginPage))
        button1.pack()


app = roomBooker()
app.geometry('1280x720')
app.configure(background='#e6eeff')
app.title('Room Booker')
app.mainloop()

在我看来,复制的show_frame函数也是不必要的。