成功登录后如何打开主窗口

问题描述 投票:0回答:3

我很开始盯着蟒蛇。我上了课程,也读了这篇文章:How To Let Your Main Window Appear after succesful login in Tkinter(PYTHON 3.6

仍然,我无法执行已撤回的事件。登录后,我想打开desctop应用程序的新(主)窗口。由于某种原因,脚本还会突然打开第三个窗口。自从2 das开始处理这些东西以来,我感到非常沮丧...

感谢您的帮助:)

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()
python python-3.x window frame new-operator
3个回答
0
投票

创建新的root = Tk()实例时,将自动打开一个小的默认窗口。

1st)您具有root = Tk()

2nd)您已经初始化了master

3rd)您有main_win

因此您有三个窗口

您必须避免在类之外再次调用类Tk(),这是令人讨厌的第三个窗口问题。

我建议做的是添加一个def,它将仅主循环您的AirsoftGunRack窗口。

这里是最终代码:

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()

注意,我在此代码中添加了四个注释。在第6,82,93,94行

此更改将打开您使用的窗口。

这些当然是我在代码块中唯一的更改。

如果影响您在tkinter项目中的表现,请对此答案发表评论。


0
投票

是,@ Jim Erginbash!这几乎就是我想要的工作方式。因此,我为主要胜利创建了一个单独的类。我考虑过认证程序。我的想法是,如果登录成功,则使用值“ true”创建一个新变量(login_completed)。在第二步中,类main_win将在取消隐藏主窗口(撤消命令)之前检查该值是否为“ true”。可悲的是,我无法正常工作。不知道在哪里添加代码以及如何添加代码。

这是到目前为止的新代码。我还重命名了一些参数以便更好地理解,它们也属于窗口。

我还注意到,通过使用框架按钮(X)关闭登录页面,它还将继续执行创建主窗口的代码。你知道怎么杀吗?

从tkinter导入Tk,标签,按钮,消息框从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

0
投票

您的认证想法很好。

在更高级的级别,您需要开始使用数据库管理tkinter应用程序(我建议使用postgresql,并从那里管理用户和密码

为了防止使用X按钮的错误,您可以在类初始化中添加以下行:

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

也将此函数添加到Login类中:(定义event_X函数)

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

这将是最终代码:

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
© www.soinside.com 2019 - 2024. All rights reserved.