如何让您的主窗口在 Tkinter(PYTHON 3.6)成功登录后出现 [英] How To Let Your Main Window Appear after succesful login in Tkinter(PYTHON 3.6

查看:26
本文介绍了如何让您的主窗口在 Tkinter(PYTHON 3.6)成功登录后出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is ui which comes with default user name and password but after successful login the main UI needs to appear

  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 新手和一般编程的新手,我将不胜感激

I would appreciate if the answers comes in functions as am newbie in python and programming in general

推荐答案

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

The below code can be used for the desired effect and is commented to show what is happening each step of the way:

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 小部件来创建一个窗口,询问用户详细信息,然后将他们定向到您可以随意设置的主窗口.

This makes use of the Toplevel widget in order to create a window which asks for the users details and then directs them to the main window which you can setup as you please.

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

You are also still able to use the pop up messages you have used in your example and if required you can also change the size of the Toplevel widget.

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

Please be advised however that this is not a particularly secure way of managing passwords and logins. As such I would suggest that you look up the proper etiquette for handling sensitive information in programming.

这篇关于如何让您的主窗口在 Tkinter(PYTHON 3.6)成功登录后出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆