简体   繁体   English

登录成功后如何打开主window

[英]How to open main window after successful login

I am gettin' stared wiith python.我开始盯着 python。 I got a udemy course and also read this post: How To Let Your Main Window Appear after succesful login in Tkinter(PYTHON 3.6我有一个udemy课程,还阅读了这篇文章: 如何让您的主要Window在Tkinter成功登录后出现(PYTHON 3.6

Still, I am unable to implement the recired event.尽管如此,我仍然无法实施 recired 事件。 I want to open a new (main) window of the desctop app after login.我想在登录后打开桌面应用程序的新(主)window。 For some reason the script also opens a third window all of a sudden.由于某种原因,该脚本还突然打开了第三个 window。 I am getting really frusted working since 2 das on that stuff...自从 2 天以来,我对那些东西的工作感到非常沮丧......

Thanks for help:)感谢帮助:)

from tkinter import Tk, Label, Button, messagebox
from tkinter import *

class AirsoftGunRack:


    ##### Main Window #####






    ##### Login Page #####


def __init__(self,master):
    """

    :type master: object
    """

    ##### Login Page #####

    self.master = master
    master.title("Login - Airsoft GunRack 3.0")
    master.geometry("450x230+450+170")


    # Creating describtions

    self.username = Label(master, text="Username:")
    self.username.place(relx=0.285, rely=0.298, height=20, width=55)

    self.password = Label(master, text="Password:")
    self.password.place(relx=0.285, rely=0.468, height=20, width=55)

    # Creating Buttons

    self.login_button = Button(master, text="Login")
    self.login_button.place(relx=0.440, rely=0.638, height=30, width=60)
    self.login_button.configure(command=self.login_user)

    self.exit_button = Button(master, text="Exit")  # , command=master.quit)
    self.exit_button.place(relx=0.614, rely=0.638, height=30, width=60)
    self.exit_button.configure(command=self.exit_login)

    # Creating entry boxes

    self.username_box = Entry(master)
    self.username_box.place(relx=0.440, rely=0.298, height=20, relwidth=0.35)

    self.password_box = Entry(master)
    self.password_box.place(relx=0.440, rely=0.468, height=20, relwidth=0.35)
    self.password_box.configure(show="*")
    self.password_box.configure(background="white")

    # Creating checkbox

    self.var = IntVar()
    self.show_password = Checkbutton(master)
    self.show_password.place(relx=0.285, rely=0.650, relheight=0.100, relwidth=0.125)
    self.show_password.configure(justify='left')
    self.show_password.configure(text='''Show''')
    self.show_password.configure(variable=self.var, command=self.cb)

def cb(self, ):

    if self.var.get() == True:
        self.password_box.configure(show="")
    else:
        self.password_box.configure(show="*")

# Giving function to login process

def login_user(self):
    name = self.username_box.get()
    password = self.password_box.get()

    if name == "user" and password == "1234":
        self.main_win.deiconify() #Unhides the root window
        self.master.destroy()  #Removes the toplevel window
        #messagebox.showinfo("Login page", "Login successful!")

    else:
        messagebox.showwarning("Login failed", "Username or password incorrect!")

def exit_login(self):
    msg = messagebox.askyesno("Exit login page", "Do you really want to exit?")

    if (msg):
        exit()




main_win = Toplevel()
main_win.title("Main Window")
main_win.title("Main Window")
main_win.geometry("800x800+450+170")

root = Tk()
gunrack = AirsoftGunRack(root)
root.mainloop()
#main_win.withdraw()
main_win.mainloop()

The moment you create a new instance of root = Tk() you will automatically get a small default window opened.当您创建root = Tk()的新实例时,您将自动打开一个小的默认 window。

1st) You have root = Tk() 1st) 你有 root = Tk()

2nd) You have the initialized master 2nd)你有初始化的主人

3rd) You have the main_win 3rd) 你有main_win

Therefore you have three windows因此你有三个 windows

You have to avoid to call the class Tk() again outside of the class and that is the annoying 3rd window problem.您必须避免在 class 之外再次调用 class Tk() ,这是令人讨厌的第三个 window 问题。

What i would suggest to do is to add a def that it will only mainloop your AirsoftGunRack window.我建议做的是添加一个定义,它只会主循环你的AirsoftGunRack window

Here is the final code:这是最终代码:

from tkinter import Tk, Label, Button, messagebox
from tkinter import *

class AirsoftGunRack:

    def __init__(self,master = Tk()): #This is my first change so i already initialize a Tk window inside the class
        """

        :type master: object
        """

        ##### Login Page #####

        self.master = master
        master.title("Login - Airsoft GunRack 3.0")
        master.geometry("450x230+450+170")


        # Creating describtions

        self.username = Label(master, text="Username:")
        self.username.place(relx=0.285, rely=0.298, height=20, width=55)

        self.password = Label(master, text="Password:")
        self.password.place(relx=0.285, rely=0.468, height=20, width=55)

        # Creating Buttons

        self.login_button = Button(master, text="Login")
        self.login_button.place(relx=0.440, rely=0.638, height=30, width=60)
        self.login_button.configure(command=self.login_user)

        self.exit_button = Button(master, text="Exit")  # , command=master.quit)
        self.exit_button.place(relx=0.614, rely=0.638, height=30, width=60)
        self.exit_button.configure(command=self.exit_login)

        # Creating entry boxes

        self.username_box = Entry(master)
        self.username_box.place(relx=0.440, rely=0.298, height=20, relwidth=0.35)

        self.password_box = Entry(master)
        self.password_box.place(relx=0.440, rely=0.468, height=20, relwidth=0.35)
        self.password_box.configure(show="*")
        self.password_box.configure(background="white")

        # Creating checkbox

        self.var = IntVar()
        self.show_password = Checkbutton(master)
        self.show_password.place(relx=0.285, rely=0.650, relheight=0.100, relwidth=0.125)
        self.show_password.configure(justify='left')
        self.show_password.configure(text='''Show''')
        self.show_password.configure(variable=self.var, command=self.cb)

    def cb(self, ):

        if self.var.get() == True:
            self.password_box.configure(show="")
        else:
            self.password_box.configure(show="*")

    # Giving function to login process

    def login_user(self):
        name = self.username_box.get()
        password = self.password_box.get()

        if name == "user" and password == "1234":
            self.main_win.deiconify() #Unhides the root window
            self.master.destroy()  #Removes the toplevel window
            #messagebox.showinfo("Login page", "Login successful!")

        else:
            messagebox.showwarning("Login failed", "Username or password incorrect!")

    def exit_login(self):
        msg = messagebox.askyesno("Exit login page", "Do you really want to exit?")
        if (msg):
            exit()

    def mainloop_window(self): #This is the class function that helps me to mainloop the window
        self.master.mainloop()




main_win = Toplevel()
main_win.title("Main Window")
main_win.title("Main Window")
main_win.geometry("800x800+450+170")

gunrack = AirsoftGunRack() # I dont need to pass the root now since its initialized inside the class
gunrack.mainloop_window() # Just mainlooping the authentication window
#main_win.withdraw()
main_win.mainloop()

Notice that i added four comments to this code.请注意,我在此代码中添加了四个注释。 In lines 6,82,93,94在第 6、82、93、94 行

This changes will open the windows you worked on.此更改将打开您处理的 windows。

Those are of course my only changes in your code block.这些当然是我对您的代码块的唯一更改。

Please comment this answer if it affects your performance in the tkinter project.如果它影响您在 tkinter 项目中的性能,请评论此答案。

Yes, @Jim Erginbash.是的,@Jim Erginbash。 That's pretty much how I want it to work, So.这就是我希望它工作的方式,所以。 I created a seperate class for main win.我为主要胜利创建了一个单独的 class 。 I thought about the authentification procedure, My idea is to create a new variable( login_completed) with the value "true".我考虑了身份验证程序,我的想法是创建一个值为“true”的新变量(login_completed)。 if login was successful.如果登录成功。 In the second step the class main_win shall check if that value is on "true" before unhiding the main window (withdraw command), Sadly.在第二步中,class main_win 应在取消隐藏主 window(撤回命令)之前检查该值是否为“真”,遗憾的是。 I cant get it work right.我不能让它正常工作。 Don't know where to add the code and how.不知道在哪里添加代码以及如何添加。

This is the new code so far.这是迄今为止的新代码。 I have also renamed some parameters for better understanding, whih window they belong too.为了更好地理解,我还重命名了一些参数,它们也属于 window。

I also noticed, that by closing the login page with the frame button (X) it will also proceed with the code creating the main window.我还注意到,通过使用框架按钮 (X) 关闭登录页面,它还将继续执行创建主 window 的代码。 Do you know how to kill it?你知道怎么杀死它吗?

from tkinter import Tk, Label, Button, messagebox from tkinter import *从 tkinter 导入 Tk,Label,按钮,从 tkinter 导入的消息框 *

    ##### Login Page #####

class Login_Page:

def __init__(self,login = Tk()): #This is my first change so i already initialize a Tk window inside the class
    """

    :type login: object
    """


    self.login = login
    login.title("Login - Airsoft GunRack 3.0")
    login.geometry("450x230+450+170")


    # Creating describtions

    self.username = Label(login, text="Username:")
    self.username.place(relx=0.285, rely=0.298, height=20, width=55)

    self.password = Label(login, text="Password:")
    self.password.place(relx=0.285, rely=0.468, height=20, width=55)

    # Creating Buttons

    self.login_button = Button(login, text="Login")
    self.login_button.place(relx=0.440, rely=0.638, height=30, width=60)
    self.login_button.configure(command=self.login_user)

    self.login_completed = IntVar()

    self.exit_button = Button(login, text="Exit")  # , command=master.quit)
    self.exit_button.place(relx=0.614, rely=0.638, height=30, width=60)
    self.exit_button.configure(command=self.exit_login)

    # Creating entry boxes

    self.username_box = Entry(login)
    self.username_box.place(relx=0.440, rely=0.298, height=20, relwidth=0.35)

    self.password_box = Entry(login)
    self.password_box.place(relx=0.440, rely=0.468, height=20, relwidth=0.35)
    self.password_box.configure(show="*")
    self.password_box.configure(background="white")

    # Creating checkbox

    self.var = IntVar()
    self.show_password = Checkbutton(login)
    self.show_password.place(relx=0.285, rely=0.650, relheight=0.100, relwidth=0.125)
    self.show_password.configure(justify='left')
    self.show_password.configure(text='''Show''')
    self.show_password.configure(variable=self.var, command=self.cb)

def cb(self, ):

    if self.var.get() == True:
        self.password_box.configure(show="")
    else:
        self.password_box.configure(show="*")

# Giving function to login process

def login_user(self):
    name = self.username_box.get()
    password = self.password_box.get()
    login_completed = self.login_completed.get()

    if name == "user" and password == "1234":
        #messagebox.showinfo("Login page", "Login successful!")
        self.login.destroy()  #Removes the toplevel window
        #self.main_win.deiconify() #Unhides the root window
        self.login_completed == 1

    else:

        messagebox.showwarning("Login Failed - Acess Denied", "Username or Password incorrect!")

        #return

def exit_login(self):
    msg = messagebox.askyesno("Exit login page", "Do you really want to exit?")
    if (msg):
        exit()

def mainloop_window(self): #This is the class function that helps me to mainloop the window
    self.login.mainloop()


login_page = Login_Page() # I dont need to pass the root now since its initialized inside the class
login_page.mainloop_window() # Just mainlooping the authentication window



   ##### Main Window #####




class Main_Win:

def __init__(self,main_win = Tk()): #This is my first change so i already initialize a Tk window inside the class
    """

    :type main_win: object
    """

    #main_win.withdraw()
    #self.main_win.deiconify() #Unhides the root window
    self.main_win = main_win
    main_win.title("Airsoft GunRack 3.0")
    main_win.geometry("900x500+250+130")







def mainloop_window(self): #This is the class function that helps me to mainloop the window
    self.main_win.mainloop()



main_win = Main_Win() # I dont need to pass the root now since its initialized inside the class

main_win.mainloop_window() # Just mainlooping the authentication window

The idea of your authentication is good.您的身份验证的想法很好。

In more advanced levels, you need to start managing your tkinter application with a database ( i suggest postgresql) and manage your users and passwords from there在更高级的级别,您需要开始使用数据库管理您的 tkinter 应用程序(我建议使用 postgresql)并从那里管理您的用户和密码

In order to prevent the bug with the X button, you can add in the class initialization the following line:为了防止 X 按钮的错误,您可以在 class 初始化中添加以下行:

login.protocol("WM_DELETE_WINDOW",self.event_X)

Also add this function in the Login class: (define the event_X function)还要在登录class中添加这个function:(定义event_X函数)

    def event_X(self):
        if messagebox.askokcancel("Exit", "Are you sure you want to exit?"):
            exit()

This will be the final code:这将是最终代码:

from tkinter import Tk, Label, Button, messagebox
from tkinter import *


##### Login Page #####

class Login_Page:

    def __init__(self, login=Tk()):  # This is my first change so i already initialize a Tk window inside the class
        """

        :type login: object
        """
        self.login = login
        login.protocol("WM_DELETE_WINDOW",self.event_X)
        login.title("Login - Airsoft GunRack 3.0")
        login.geometry("450x230+450+170")

    # Creating describtioneves

        self.username = Label(login, text="Username:")
        self.username.place(relx=0.285, rely=0.298, height=20, width=55)

        self.password = Label(login, text="Password:")
        self.password.place(relx=0.285, rely=0.468, height=20, width=55)

        # Creating Buttons

        self.login_button = Button(login, text="Login")
        self.login_button.place(relx=0.440, rely=0.638, height=30, width=60)
        self.login_button.configure(command=self.login_user)

        self.login_completed = IntVar()

        self.exit_button = Button(login, text="Exit")  # , command=master.quit)
        self.exit_button.place(relx=0.614, rely=0.638, height=30, width=60)
        self.exit_button.configure(command=self.exit_login)

        # Creating entry boxes

        self.username_box = Entry(login)
        self.username_box.place(relx=0.440, rely=0.298, height=20, relwidth=0.35)

        self.password_box = Entry(login)
        self.password_box.place(relx=0.440, rely=0.468, height=20, relwidth=0.35)
        self.password_box.configure(show="*")
        self.password_box.configure(background="white")

        # Creating checkbox

        self.var = IntVar()
        self.show_password = Checkbutton(login)
        self.show_password.place(relx=0.285, rely=0.650, relheight=0.100, relwidth=0.125)
        self.show_password.configure(justify='left')
        self.show_password.configure(text='''Show''')
        self.show_password.configure(variable=self.var, command=self.cb)

    def event_X(self):
        if messagebox.askokcancel("Exit", "Are you sure you want to exit?"):
            exit()

    def cb(self, ):
        if self.var.get() == True:
            self.password_box.configure(show="")
        else:
            self.password_box.configure(show="*")


# Giving function to login process

    def login_user(self):
        name = self.username_box.get()
        password = self.password_box.get()
        login_completed = self.login_completed.get()

        if name == "user" and password == "1234":
            # messagebox.showinfo("Login page", "Login successful!")
            self.login.destroy()  # Removes the toplevel window
            # self.main_win.deiconify() #Unhides the root window
            self.login_completed == 1

        else:
            messagebox.showwarning("Login Failed - Acess Denied", "Username or Password incorrect!")

        # return


    def exit_login(self):
        msg = messagebox.askyesno("Exit login page", "Do you really want to exit?")
        if (msg):
            exit()


    def mainloop_window(self):  # This is the class function that helps me to mainloop the window
        self.login.mainloop()


login_page = Login_Page()  # I dont need to pass the root now since its initialized inside the class
login_page.mainloop_window()  # Just mainlooping the authentication window


    ##### Main Window #####


class Main_Win:
    def __init__(self, main_win=Tk()):  # This is my first change so i already initialize a Tk window inside the class
        self.main_win = main_win
        main_win.title("Airsoft GunRack 3.0")
        main_win.geometry("900x500+250+130")


    def mainloop_window(self):  # This is the class function that helps me to mainloop the window
        self.main_win.mainloop()


main_win = Main_Win()  # I dont need to pass the root now since its initialized inside the class
main_win.mainloop_window()  # Just mainlooping the authentication window

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM