python-3.x 单击关闭一个类时,如何调用另一个类?Tkinter

nukf8bse  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(125)

我想做的是在关闭一个页面后打开一个页面。当我完成了第二个页面,我希望第一个页面再次打开时,点击提交。我为每个页面使用了不同的类,所以我不知道如何解决这个问题。
我尝试将第一个类作为参数传递给第二个类。当我关闭第一页时,我能够打开第二页,但当我关闭第二页时,我无法打开第一页。我希望第一页打开后,关闭第二个以及。
'''Python

class WelcomePage():
#The first page

def __init__(self):
    self.window = tk.Tk()
    self.window.title("Welcome!")
    self.window.geometry("300x300")
    self.window.configure(bg="light green")

    self.welcome_frame = tk.LabelFrame(self.window,
                                       background="light blue",
                                       foreground="light blue")
    self.welcome_frame.pack(pady=15)

    self.welcome_label = tk.Label(self.welcome_frame, text="WELCOME!",
                                  background="light grey",
                                  foreground="blue")
    self.welcome_label.pack()

    self.account_dropbox_frame = tk.LabelFrame(self.window)
    self.account_dropbox_frame.configure(bg="light blue",
                                         fg="light blue")
    
    self.account_dropbox_frame.pack(pady=15)

    self.account_dropbox_var = tk.StringVar(self.window)
    self.account_dropbox_var.set("one")
    self.account_dropbox = tk.OptionMenu(self.account_dropbox_frame, self.account_dropbox_var,
                                         "one", "two", "three")
    
    self.account_dropbox.configure(bg="light blue",
                                   fg="blue",
                                   highlightbackground="light blue")
    self.account_dropbox.pack()

    self.group_dropbox_frame = tk.LabelFrame(self.window)
    self.group_dropbox_frame.configure(bg="light blue",
                                       fg="light blue")
    
    self.group_dropbox_frame.pack(pady=15)

    self.group_dropbox_var = tk.StringVar()
    self.group_dropbox_var.set("one")
    self.group_dropbox = tk.OptionMenu(self.group_dropbox_frame,
                                       self.group_dropbox_var,
                                       "one", "two", "three")
    
    self.group_dropbox.configure(bg="light blue",
                                 fg="blue",
                                 highlightbackground="light blue")
    self.group_dropbox.pack()

    self.button_frame = tk.LabelFrame(self.window)
    self.button_frame.configure(bg="light blue")
    self.button_frame.pack()

    self.button_frame.columnconfigure(0, weight=1)
    self.button_frame.columnconfigure(1, weight=1)
    self.button_frame.rowconfigure(0, weight=1)

    self.new_account_button = tk.Button(self.button_frame,
                                        text="New Account",
                                        command=self.start_account_page,
                                        foreground="blue",
                                        background="light blue")
    
    self.new_group_button = tk.Button(self.button_frame,
                                      text="New Group",
                                      foreground="blue",
                                      background="light blue")

    self.new_account_button.grid(row=0, column=0)
    self.new_group_button.grid(row=0, column=1)

    self.load = Image.open(r"C:\Users\pc\Desktop\Telegram_bot\right_arrow.jpg").convert("RGBA")
    self.resized_img = self.load.resize((25,25))
    
    self.render = ImageTk.PhotoImage(self.resized_img)
    self.click_btn = tk.Button(self.window, image=self.render)
    self.click_btn.place(relx= 0.90, rely=0.90)

def start_account_page(self):
    new_account_page = NewAccountPage(main_window=self.window)
    self.window.destroy()
    new_account_page.run()
    
    

def run(self):
    self.window.mainloop()

'''
'''Python

class NewAccountPage():
#the second page

def __init__(self, main_window):
    self.main_window = main_window
    self.window = tk.Tk()
    self.window.title("New Account")
    self.window.geometry("300x300")

    self.window.rowconfigure(0, weight=1)
    self.window.rowconfigure(1, weight=1)
    self.window.rowconfigure(2, weight=1)
    self.window.rowconfigure(3, weight=1)
    self.window.columnconfigure(0, weight=1)

    self.new_account_label = tk.Label(self.window, text="NEW ACCOUNT", foreground="red")
    self.new_account_label.grid(row=0, column=0)

    self.entry_frame = tk.LabelFrame(self.window)
    self.entry_frame.grid(row=1, column=0)

    self.api_hash_label = tk.Label(self.entry_frame, text="Enter API Hash")
    self.api_hash_label.pack()

    self.api_hash_entry = tk.Entry(self.entry_frame)
    self.api_hash_entry.pack()

    self.api_id_label = tk.Label(self.entry_frame, text="Enter API ID")
    self.api_id_label.pack()

    self.api_id_entry = tk.Entry(self.entry_frame)
    self.api_id_entry.pack()

    self.phone_label = tk.Label(self.entry_frame, text="Enter Phone Number")
    self.phone_label.pack()

    self.phone_entry = tk.Entry(self.entry_frame)
    self.phone_entry.pack()

    self.submit_button = tk.Button(self.window, text="Submit", command=self.submit_account)
    self.submit_button.grid(row=2, column=0)

def submit_account(self):
    self.window.destroy()
    self.main_window.mainloop()

def run(self):
    self.window.mainloop()

'''

new9mtju

new9mtju1#

当切换到其他窗口时,你不需要销毁根窗口,只需隐藏它。关闭其他窗口后,再次显示根窗口。
对根窗口以外的窗口使用Toplevelmainloop()也应该执行一次。

class NewAccountPage():
    def __init__(self, main_window):
        self.main_window = main_window
        self.window = tk.Toplevel()   # use Toplevel instead of Tk here
        # make sure to call submit_account() when closing the window
        # using the close button in the title bar
        self.window.protocol("WM_DELETE_WINDOW", self.submit_account)
        ...

    def submit_account(self):
        self.window.destroy()
        self.main_window.deiconify()  # show the root window
class WelcomePage():
    ...

    def start_account_page(self):
        self.window.withdraw()   # hide the root window
        NewAccountPage(main_window=self.window)

    ...

相关问题