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

Python 如何在成功登录后打开主窗口,python,python-3.x,window,frame,new-operator,Python,Python 3.x,Window,Frame,New Operator,我正在用巨蟒。我在我的课程中获得了一个学位,也读了这篇文章: 但是,我无法实现recired事件。我想在登录后打开desctop应用程序的新(主)窗口。出于某种原因,脚本还会突然打开第三个窗口。我真的很沮丧,因为2个das的工作在那东西 谢谢你的帮助:) 创建root=Tk()的新实例时,将自动打开一个小的默认窗口 1)您有root=Tk() 第二)您拥有初始化的主机 第三)您拥有主要胜利 因此,您有三个窗口 您必须避免在类之外再次调用类Tk(),这是令人讨厌的第三个窗口问题 我建议做的是添加一

我正在用巨蟒。我在我的课程中获得了一个学位,也读了这篇文章:

但是,我无法实现recired事件。我想在登录后打开desctop应用程序的新(主)窗口。出于某种原因,脚本还会突然打开第三个窗口。我真的很沮丧,因为2个das的工作在那东西

谢谢你的帮助:)


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

1)您有root=Tk()

第二)您拥有初始化的主机

第三)您拥有主要胜利

因此,您有三个窗口

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

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

以下是最终代码:

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
从tkinter导入Tk、标签、按钮、消息框
从tkinter进口*
空气软支架等级:
def uu init(self,master=Tk()):#这是我的第一个更改,所以我已经在类中初始化了一个Tk窗口
"""
:类型主控:对象
"""
#####登录页面#####
self.master=master
主标题(“登录-Airsoft GunRack 3.0”)
主几何(“450x230+450+170”)
#创建描述
self.username=标签(master,text=“username:”)
self.username.place(relx=0.285,relen=0.298,高度=20,宽度=55)
self.password=标签(master,text=“password:)
self.password.place(relx=0.285,rely=0.468,高度=20,宽度=55)
#创建按钮
self.login\u button=按钮(master,text=“login”)
self.login_按钮位置(relx=0.440,rely=0.638,高度=30,宽度=60)
self.login\u按钮.configure(命令=self.login\u用户)
self.exit_按钮=按钮(master,text=“exit”)#,command=master.quit)
自动退出按钮位置(relx=0.614,RELn=0.638,高度=30,宽度=60)
self.exit\u按钮.configure(命令=self.exit\u登录)
#创建输入框
self.username\u box=条目(主)
self.username\u box.place(relx=0.440,rely=0.298,高度=20,relwidth=0.35)
self.password\u box=输入(主)
self.password_box.place(relx=0.440,rely=0.468,高度=20,relwidth=0.35)
self.password\u box.configure(show=“*”)
self.password\u box.configure(background=“white”)
#创建复选框
self.var=IntVar()
self.show_password=检查按钮(主控)
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(文本='''show'')
self.show\u password.configure(变量=self.var,命令=self.cb)
def cb(self,):
如果self.var.get()==True:
self.password\u box.configure(show=”“)
其他:
self.password\u box.configure(show=“*”)
#赋予登录过程功能
def登录用户(自身):
name=self.username\u box.get()
password=self.password\u box.get()
如果名称=“用户”和密码=“1234”:
self.main_win.deiconify()#取消隐藏根窗口
self.master.destroy()#删除顶层窗口
#showinfo(“登录页面”,“登录成功!”)
其他:
messagebox.showwarning(“登录失败”,“用户名或密码不正确!”)
def退出_登录(自我):
msg=messagebox.askyesno(“退出登录页面”,“您真的想退出吗?”)
如果(味精):
退出()
def mainloop_window(self):#这是帮助我主循环窗口的类函数
self.master.mainloop()
main_win=顶级()
主窗口标题(“主窗口”)
主窗口标题(“主窗口”)
主温几何(“800x800+450+170”)
gunrack=AirsoftGunRack()#我现在不需要传递根目录,因为它在类内部初始化
gunrack.mainloop_window()#只需mainloop身份验证窗口
#main_win.draw()
main_win.mainloop()
请注意,我在这段代码中添加了四条注释。第6、82、93、94行

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

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


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

是的,@Jim Erginbash!这正是我所希望的。所以,我为main win创建了一个单独的类。我考虑过认证程序。如果登录成功,我的想法是创建一个值为“true”的新变量(login_completed)。在第二步中,在取消隐藏主窗口(撤销命令)之前,类main_win应检查该值是否为“真”。遗憾的是,我不能让它正常工作。不知道在哪里添加代码以及如何添加

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

我还注意到,通过使用框架按钮(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

身份验证的想法很好

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

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

login.protocol("WM_DELETE_WINDOW",self.event_X)
还可以在Login类中添加此函数:(定义event_X函数)

这将是最终代码:

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

谢谢你的告别。可悲的是,它也不能按我想要的方式正常工作。在登录成功之前,应隐藏主窗口。此外,主窗口将有很多内容,因为它将是m
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