2017-03-20 70 views
0

所以這是我的代碼下面的登錄菜單,一旦用戶登錄我想登錄菜單被隱藏或刪除,我試過.withdraw()和.destroy(),即時通訊確定即時通訊將代碼放入錯誤的地方,任何援助將不勝感激!如何打開頂層時刪除登錄窗口?

from tkinter import * 
import tkinter as tk 
import sqlite3 
import hashlib 
import os 
import weakref 


def main(): 
    root = Tk() 
    width = 600 #sets the width of the window 
    height = 600 #sets the height of the window 
    widthScreen = root.winfo_screenwidth() #gets the width of the screen 
    heightScreen = root.winfo_screenheight() #gets the height of the screen 
    x = (widthScreen/2) - (width/2) #finds the center value of x 
    y = (heightScreen/2) - (height/2) #finds the center value of y 
    root.geometry('%dx%d+%d+%d' % (width, height, x, y))#places screen in center 
    root.resizable(width=False, height=False)#Ensures that the window size cannot be changed 
    filename = PhotoImage(file = 'Login.gif') #gets the image from directory 
    background_label = Label(image=filename) #makes the image 
    background_label.place(x=0, y=0, relwidth=1, relheight=1)#palces the image 
    logins = login(root) 
    root.mainloop() 

class login(Tk): 
    def __init__(self, master): 
    self.__username = StringVar() 
    self.__password = StringVar() 
    self.__error = StringVar() 
    self.master = master 
    self.master.title('Login') 
    userNameLabel = Label(self.master, text='UserID: ', bg=None, width=10).place(relx=0.300,rely=0.575) 
    userNameEntry = Entry(self.master, textvariable=self.__username, width=25).place(relx=0.460,rely=0.575) 
    userPasswordLabel = Label(self.master, text='Password: ', bg=None, width=10).place(relx=0.300,rely=0.625) 
    userPasswordEntry = Entry(self.master, textvariable=self.__password, show='*', width=25).place(relx=0.460,rely=0.625) 
    errorLabel = Label(self.master, textvariable=self.__error, bg=None, fg='red', width=35).place(relx=0.508,rely=0.545, anchor=CENTER) 
    loginButton = Button(self.master, text='Login', command=self.login_user, bg='white', width=15).place(relx=0.300,rely=0.675) 
    clearButton = Button(self.master, text='Clear', command=self.clear_entry, bg='white', width=15).place(relx=0.525,rely=0.675) 
    self.master.bind('<Return>', lambda event: self.login_user()) #triggers the login subroutine if the enter key is pressed 

    def login_user(self): 
    username = self.__username.get() 
    password = self.__password.get() 
    hashPassword = (password.encode('utf-8')) 
    newPass = hashlib.sha256() 
    newPass.update(hashPassword) 
    if username == '': 
     self.__error.set('Error! No user ID entered') 
    elif password == '': 
     self.__error.set('Error! No password entered') 
    else: 
     with sqlite3.connect ('pkdata.db') as db: 
     cursor = db.cursor() 
     cursor.execute("select userID, password from users where userID=?",(username,)) 
     info = cursor.fetchone() 
     if info is None: 
      self.__error.set('Error! Login details not found!') 
     else: 
      dbUsername = info[0] 
      dbPassword = info[1] 
      if username == dbUsername or newPass.hexdigest() == dbPassword: 
      self.open_main_menu() 
      self.master.withdraw() 
      else: 
      self.__error.set('Error! please try again') 
      self.clear_entry() 

    def destroy(self): 
    self.master.destroy() 

    def clear_entry(self): 
    self.__username.set('') 
    self.__password.set('') 

    def open_main_menu(self): 
    root_main = Toplevel(self.master) 
    root_main.state('zoomed') 
    Main = main_menu(root_main) 
    root_main.mainloop() 


class main_menu(): 
    def __init__(self, master): 
    pass 

if __name__ == '__main__': 
    main() 
+1

1:你有兩個主循環,當你只應該最多隻能有一個。 2:登錄不應該是Tk的子類,因爲它將使登錄成爲「根」 – abccd

+0

Naseem,我從您的個人資料中看到,您已經獲得了幾個問題的答案,但是您沒有接受或投票支持任何一個問題。我建議閱讀[我應該怎麼做當有人回答我的問題?](http://stackoverflow.com/help/someone-answers) –

+0

我可以upvote一個答案,但我有更少的15代表因此不公開。會贊成那些有幫助的人。感謝Nas –

回答

0

您的代碼中的一個問題是您不止一次地致電mainloop。作爲一般規則,您應該始終在tkinter GUI中調用mainloop一次。

執行登錄窗口的最常見方法是使登錄窗口成爲Toplevel的實例,並將主GUI程序放在根窗口中。在啓動時,您可以隱藏主窗口並顯示登錄窗口。如果登錄成功,隱藏登錄窗口並顯示根窗口。

它看起來是這樣的:

import tkinter as tk 

class Login(tk.Toplevel): 
    def __init__(self, root): 
     super().__init__(root) 

     self.root = root 

     username_label = tk.Label(self, text="Username:") 
     password_label = tk.Label(self, text="Password:") 
     username_entry = tk.Entry(self) 
     password_entry = tk.Entry(self) 
     submit = tk.Button(self, text="Login", command=self.login) 

     username_label.grid(row=0, column=0, sticky="e") 
     username_entry.grid(row=0, column=1, sticky="ew") 
     password_label.grid(row=1, column=0, sticky="e") 
     password_entry.grid(row=1, column=1, sticky="ew") 
     submit.grid(row=2, column=1, sticky="e") 

    def login(self): 
     # if username and password are valid: 
     self.withdraw() 
     self.root.deiconify() 

def main(): 
    root = tk.Tk() 
    root.withdraw() 

    label = tk.Label(root, text="this is the main application") 
    label.pack(fill="both", expand=True, padx=100, pady=100) 

    login_window = Login(root) 

    root.mainloop() 

if __name__ == "__main__": 
    main() 
+0

感謝:)非常感謝 –

+0

只需調整上面的答案代碼以適應我自己的需求,您是否可以解釋爲什麼root是一個全局變量?在此先感謝:) –

+0

@NaseemTomkinson:它不需要。那是個錯誤。它是從早期版本中留下的。我會解決它。 –