如何在用户登录tkinter Py3之前撤消缩放状态窗口
我在寻找一种方法,一旦用户登录,我就可以在缩放状态下打开一个新窗口。这将是主菜单,从代码行11的顶部调用。我可以打开这个新窗口,如果不使用“缩放”状态,也可以取消隐藏它。如果该窗口处于缩放状态,我将如何打开/取消隐藏该窗口?出于某种原因,它会在程序启动时打开它,即使在使用缩放状态时用户没有登录如何在用户登录tkinter Py3之前撤消缩放状态窗口,tkinter,Tkinter,我在寻找一种方法,一旦用户登录,我就可以在缩放状态下打开一个新窗口。这将是主菜单,从代码行11的顶部调用。我可以打开这个新窗口,如果不使用“缩放”状态,也可以取消隐藏它。如果该窗口处于缩放状态,我将如何打开/取消隐藏该窗口?出于某种原因,它会在程序启动时打开它,即使在使用缩放状态时用户没有登录 from tkinter import * import sqlite3 import hashlib import os def main(): root = Tk() root.withdr
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()
如果你想打开两个窗口,Toplevel是不错的,但那不是你想要的,是吗?您希望打开一个窗口,然后再打开另一个窗口。因此,与其定义两个窗口,不如定义一个窗口,然后在适当的时候更改它: 类Mainframetk.Tk: 定义初始自我: tk.tk.\uuuuu init\uuuu self self.frame=FirstFrameself 自我框架包装 def changeself,帧: self.frame.pack\u忘记删除当前帧 self.frame=frameself self.frame.pack制作新框架 类FirstFrametk.Frame: 定义初始自我,主控=无,**kwargs: tk.帧。初始帧自身,主帧,**kwargs master.titleEnter密码 master.geometry300x200 self.status=tk.Labelself,fg='red' self.status.pack lbl=tk.Labelself,text='Enter password' lbl.pack self.pwd=tk.Entryself,显示=* 自我包装 自我聚焦 自检 btn=tk.Buttonself,text=Done,command=self.check btn.pack btn=tk.Buttonself,text=Cancel,command=self.quit btn.pack def checkself,事件=无: 如果self.pwd.get==“密码”: self.master.changessecondframe 其他: self.status.configtext=密码错误 类SecondFrametk.Frame: 定义初始自我,主控=无,**kwargs: tk.帧。初始帧自身,主帧,**kwargs 标题主应用程序 master.geometry600x400 lbl=tk.Labelself,text='您成功地访问了主应用程序' lbl.pack 如果uuuu name uuuuu==\uuuuuuuu main\uuuuuuuu: app=大型机 app.mainloop