我正在寻找一种方法,一旦用户登录,我就可以在缩放状态下打开一个新窗口。这将是main_menu,这是从代码顶部调用的(第11行)。我可以打开这个新窗口,或者如果缩放的话,取消隐藏它。状态未被使用。如果它处于缩放状态,我将如何打开/取消隐藏此窗口?由于某种原因,它会在程序启动时打开它,即使没有用户登录,如果使用了缩放状态。
from tkinter import *
import sqlite3
import hashlib
import os
def main():
root = Tk()
root.withdraw()
mainMenu = main_menu(root)
loginWindow = login(root)
root.mainloop()
class login(Toplevel):
def __init__(self, root):
super().__init__(root)
width = 600 #sets the width of the window
height = 600 #sets the height of the window
widthScreen = self.winfo_screenwidth() #gets the width of the screen
heightScreen = self.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
self.geometry('%dx%d+%d+%d' % (width, height, x, y))#places screen in center
self.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
self.__username = StringVar()
self.__password = StringVar()
self.__error = StringVar()
userNameLabel = Label(self, text='UserID: ', bg=None, width=10).place(relx=0.300,rely=0.575)
userNameEntry = Entry(self, textvariable=self.__username, width=25).place(relx=0.460,rely=0.575)
userPasswordLabel = Label(self, text='Password: ', bg=None, width=10).place(relx=0.300,rely=0.625)
userPasswordEntry = Entry(self, textvariable=self.__password, show='*', width=25).place(relx=0.460,rely=0.625)
errorLabel = Label(self, textvariable=self.__error, bg=None, fg='red', width=35).place(relx=0.508,rely=0.545, anchor=CENTER)
loginButton = Button(self, text='Login', command=self.login_user, bg='white', width=15).place(relx=0.300,rely=0.675)
clearButton = Button(self, 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:
loginWindow.withdraw()
root.deiconify()
else:
self.__error.set('Error! please try again')
self.clear_entry()
def clear_entry(self):
self.__username.set('')
self.__password.set('')
class main_menu():
def __init__(self, root):
self.root = root
root.state('zoomed')
root.title("Main Menu")
if __name__ == '__main__':
main()
答案 0 :(得分:0)
如果你想打开2个窗户,Toplevel是好的,但这不是你想要的吗?您希望打开一个窗口,然后打开另一个窗口。因此,不是定义2个窗口,而是定义单个窗口然后在时机成熟时更改它会容易得多:
class Mainframe(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.frame = FirstFrame(self)
self.frame.pack()
def change(self, frame):
self.frame.pack_forget() # delete currrent frame
self.frame = frame(self)
self.frame.pack() # make new frame
class FirstFrame(tk.Frame):
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
master.title("Enter password")
master.geometry("300x200")
self.status = tk.Label(self, fg='red')
self.status.pack()
lbl = tk.Label(self, text='Enter password')
lbl.pack()
self.pwd = tk.Entry(self, show="*")
self.pwd.pack()
self.pwd.focus()
self.pwd.bind('<Return>', self.check)
btn = tk.Button(self, text="Done", command=self.check)
btn.pack()
btn = tk.Button(self, text="Cancel", command=self.quit)
btn.pack()
def check(self, event=None):
if self.pwd.get() == 'password':
self.master.change(SecondFrame)
else:
self.status.config(text="wrong password")
class SecondFrame(tk.Frame):
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
master.title("Main application")
master.geometry("600x400")
lbl = tk.Label(self, text='You made it to the main application')
lbl.pack()
if __name__=="__main__":
app=Mainframe()
app.mainloop()