2017-10-04 24 views
-1

パスワードの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

_password_checkの下であなたのshow_frame呼び出しが間違っていた、事前にありがとうございます。これを試してください:

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関数は私の意見では不要です。

+0

ありがとうございました!私はそれが「アプリ」だとは信​​じられません。私に非常に多くの問題を引き起こしました。 – ChocolateSteak

関連する問題