一个问题有很多...我会尽力告诉您如何实现的。我通常不创建一个新窗口。我只是更改已经存在的窗口。
from tkinter import *
root = Tk()
# Main window of Application
root.title("Please Login")
root.geometry("300x150")
root.config(background='lightblue',borderwidth=5)
# Possible Login
possible_users = {'user1': 'user1_pass','user2': 'user2_pass'} # dictionary of corresponding user name and passwords
# StringVars
the_user = StringVar() # used to retrieve input from entry
the_pass = StringVar()
# Creating Label Widget
myLabel1 = Label(root,text="Username :",background='lightblue')
myLabel2 = Label(root,text="Password :",background='lightblue')
bad_pass = Label(root,text="Incorrect Username or Password")
# Entry fields
username_1 = Entry(root,textvariable=the_user)
password_1 = Entry(root,show='*',textvariable=the_pass)
# Putting labels onto screen
myLabel1.grid(row=0,column=0)
myLabel2.grid(row=1,column=0)
# Entry field Locations
username_1.grid(row=0,column=1)
password_1.grid(row=1,column=1)
def login(user):
forget_login_window()
next_window(user)
def check_login():
requested_user = the_user.get()
try:
if possible_users[requested_user] == the_pass.get():
login(requested_user)
else:
bad_pass.grid(row=2,column=1)
except KeyError:
bad_pass.grid(row=2,column=1)
loginButton1 = Button(root,text="Login",command=check_login)
cancelButton3 = Button(root,text="Cancel",command=quit)
# Putting buttons onto screen
loginButton1.grid(row=6,column=1)
cancelButton3.grid(row=7,column=1)
# New window
def forget_login_window(): # forget all the grid items.
username_1.grid_forget()
password_1.grid_forget()
myLabel1.grid_forget()
myLabel2.grid_forget()
loginButton1.grid_forget()
cancelButton3.grid_forget()
bad_pass.grid_forget()
def next_window(my_user):
root.title(my_user) # desired changes here
# you will need to create your tkinter objects (buttons,labels etc) in global and pack / grid / place them here.
root.mainloop()
本文链接:https://www.f2er.com/3149199.html