如何让你的主窗口在 Tkinter 成功登录后出现(PYTHON 3.6

2024-04-05

这是带有默认用户名和密码的用户界面,但成功登录后需要出现主用户界面

挑战

  1. 当您正确输入用户名和密码时,主窗口不会打开,而是当您单击取消或关闭按钮时,它会打开

寻找解决方案

  1. 使用默认密码和用户名成功登录后应出现主窗口
from tkinter import *
from tkinter import ttk
from tkinter import messagebox


def try_login():               # this my login function  
    if name_entry.get()==default_name and password_entry.get() == 
       default_password:
       messagebox.showinfo("LOGIN SUCCESSFULLY","WELCOME")
    else:
       messagebox.showwarning("login failed","Please try again" )


def cancel_login():        # exit function
    log.destroy()


default_name=("user")      #DEFAULT LOGIN ENTRY
default_password=("py36")


log=Tk()                   #this login ui
log.title("ADMIN-LOGIN")
log.geometry("400x400+400+200")
log.resizable (width=FALSE,height=FALSE)


LABEL_1 = Label(log,text="USER NAME")
LABEL_1.place(x=50,y=100)
LABEL_2 = Label(log,text="PASSWORD")
LABEL_2.place(x=50,y=150)

BUTTON_1=ttk. Button(text="login",command=try_login)
BUTTON_1.place(x=50,y=200)
BUTTON_1=ttk. Button(text="cancel",command=cancel_login)
BUTTON_1.place(x=200,y=200)

name_entry=Entry(log,width=30)
name_entry.place(x=150,y=100)
password_entry=ttk. Entry(log,width=30,show="*")
password_entry.place(x=150,y=150)

log. mainloop()



MAIN_WINDOW=Tk()      #after successful this main ui should appear

MAIN_WINDOW.geometry("600x500+300+100")

MENU_1 = Menu(MAIN_WINDOW)
MAIN_WINDOW.config(menu=MENU_1)

SETTINGS_1 = Menu(MENU_1,tearoff=0)
MENU_1.add_cascade(label="SETTINGS",menu=SETTINGS_1,underline=0)
SETTINGS_1.add_command(label="Change Password")

MAIN_WINDOW. mainloop()

如果答案像 python 和一般编程新手一样以函数形式出现,我将不胜感激


下面的代码可用于达到所需的效果,并带有注释以显示每一步发生的情况:

from tkinter import * #Imports Tkinter
import sys #Imports sys, used to end the program later

root=Tk() #Declares root as the tkinter main window
top = Toplevel() #Creates the toplevel window

entry1 = Entry(top) #Username entry
entry2 = Entry(top) #Password entry
button1 = Button(top, text="Login", command=lambda:command1()) #Login button
button2 = Button(top, text="Cancel", command=lambda:command2()) #Cancel button
label1 = Label(root, text="This is your main window and you can input anything you want here")

def command1():
    if entry1.get() == "user" and entry2.get() == "password": #Checks whether username and password are correct
        root.deiconify() #Unhides the root window
        top.destroy() #Removes the toplevel window

def command2():
    top.destroy() #Removes the toplevel window
    root.destroy() #Removes the hidden root window
    sys.exit() #Ends the script


entry1.pack() #These pack the elements, this includes the items for the main window
entry2.pack()
button1.pack()
button2.pack()
label1.pack()

root.withdraw() #This hides the main window, it's still present it just can't be seen or interacted with
root.mainloop() #Starts the event loop for the main window

这利用了Toplevel小部件,以便创建一个窗口,询问用户详细信息,然后将他们定向到您可以根据需要进行设置的主窗口。

您仍然可以使用示例中使用的弹出消息,如果需要,您还可以更改弹出消息的大小Toplevel widget.

但请注意,这并不是一种特别安全的管理密码和登录的方式。因此,我建议您查找在编程中处理敏感信息的正确礼仪。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让你的主窗口在 Tkinter 成功登录后出现(PYTHON 3.6 的相关文章

随机推荐