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

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

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

  1. class WelcomePage():
  2. #The first page
  3. def __init__(self):
  4. self.window = tk.Tk()
  5. self.window.title("Welcome!")
  6. self.window.geometry("300x300")
  7. self.window.configure(bg="light green")
  8. self.welcome_frame = tk.LabelFrame(self.window,
  9. background="light blue",
  10. foreground="light blue")
  11. self.welcome_frame.pack(pady=15)
  12. self.welcome_label = tk.Label(self.welcome_frame, text="WELCOME!",
  13. background="light grey",
  14. foreground="blue")
  15. self.welcome_label.pack()
  16. self.account_dropbox_frame = tk.LabelFrame(self.window)
  17. self.account_dropbox_frame.configure(bg="light blue",
  18. fg="light blue")
  19. self.account_dropbox_frame.pack(pady=15)
  20. self.account_dropbox_var = tk.StringVar(self.window)
  21. self.account_dropbox_var.set("one")
  22. self.account_dropbox = tk.OptionMenu(self.account_dropbox_frame, self.account_dropbox_var,
  23. "one", "two", "three")
  24. self.account_dropbox.configure(bg="light blue",
  25. fg="blue",
  26. highlightbackground="light blue")
  27. self.account_dropbox.pack()
  28. self.group_dropbox_frame = tk.LabelFrame(self.window)
  29. self.group_dropbox_frame.configure(bg="light blue",
  30. fg="light blue")
  31. self.group_dropbox_frame.pack(pady=15)
  32. self.group_dropbox_var = tk.StringVar()
  33. self.group_dropbox_var.set("one")
  34. self.group_dropbox = tk.OptionMenu(self.group_dropbox_frame,
  35. self.group_dropbox_var,
  36. "one", "two", "three")
  37. self.group_dropbox.configure(bg="light blue",
  38. fg="blue",
  39. highlightbackground="light blue")
  40. self.group_dropbox.pack()
  41. self.button_frame = tk.LabelFrame(self.window)
  42. self.button_frame.configure(bg="light blue")
  43. self.button_frame.pack()
  44. self.button_frame.columnconfigure(0, weight=1)
  45. self.button_frame.columnconfigure(1, weight=1)
  46. self.button_frame.rowconfigure(0, weight=1)
  47. self.new_account_button = tk.Button(self.button_frame,
  48. text="New Account",
  49. command=self.start_account_page,
  50. foreground="blue",
  51. background="light blue")
  52. self.new_group_button = tk.Button(self.button_frame,
  53. text="New Group",
  54. foreground="blue",
  55. background="light blue")
  56. self.new_account_button.grid(row=0, column=0)
  57. self.new_group_button.grid(row=0, column=1)
  58. self.load = Image.open(r"C:\Users\pc\Desktop\Telegram_bot\right_arrow.jpg").convert("RGBA")
  59. self.resized_img = self.load.resize((25,25))
  60. self.render = ImageTk.PhotoImage(self.resized_img)
  61. self.click_btn = tk.Button(self.window, image=self.render)
  62. self.click_btn.place(relx= 0.90, rely=0.90)
  63. def start_account_page(self):
  64. new_account_page = NewAccountPage(main_window=self.window)
  65. self.window.destroy()
  66. new_account_page.run()
  67. def run(self):
  68. self.window.mainloop()

'''
'''Python

  1. class NewAccountPage():
  2. #the second page
  3. def __init__(self, main_window):
  4. self.main_window = main_window
  5. self.window = tk.Tk()
  6. self.window.title("New Account")
  7. self.window.geometry("300x300")
  8. self.window.rowconfigure(0, weight=1)
  9. self.window.rowconfigure(1, weight=1)
  10. self.window.rowconfigure(2, weight=1)
  11. self.window.rowconfigure(3, weight=1)
  12. self.window.columnconfigure(0, weight=1)
  13. self.new_account_label = tk.Label(self.window, text="NEW ACCOUNT", foreground="red")
  14. self.new_account_label.grid(row=0, column=0)
  15. self.entry_frame = tk.LabelFrame(self.window)
  16. self.entry_frame.grid(row=1, column=0)
  17. self.api_hash_label = tk.Label(self.entry_frame, text="Enter API Hash")
  18. self.api_hash_label.pack()
  19. self.api_hash_entry = tk.Entry(self.entry_frame)
  20. self.api_hash_entry.pack()
  21. self.api_id_label = tk.Label(self.entry_frame, text="Enter API ID")
  22. self.api_id_label.pack()
  23. self.api_id_entry = tk.Entry(self.entry_frame)
  24. self.api_id_entry.pack()
  25. self.phone_label = tk.Label(self.entry_frame, text="Enter Phone Number")
  26. self.phone_label.pack()
  27. self.phone_entry = tk.Entry(self.entry_frame)
  28. self.phone_entry.pack()
  29. self.submit_button = tk.Button(self.window, text="Submit", command=self.submit_account)
  30. self.submit_button.grid(row=2, column=0)
  31. def submit_account(self):
  32. self.window.destroy()
  33. self.main_window.mainloop()
  34. def run(self):
  35. self.window.mainloop()

'''

new9mtju

new9mtju1#

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

  1. class NewAccountPage():
  2. def __init__(self, main_window):
  3. self.main_window = main_window
  4. self.window = tk.Toplevel() # use Toplevel instead of Tk here
  5. # make sure to call submit_account() when closing the window
  6. # using the close button in the title bar
  7. self.window.protocol("WM_DELETE_WINDOW", self.submit_account)
  8. ...
  9. def submit_account(self):
  10. self.window.destroy()
  11. self.main_window.deiconify() # show the root window
  1. class WelcomePage():
  2. ...
  3. def start_account_page(self):
  4. self.window.withdraw() # hide the root window
  5. NewAccountPage(main_window=self.window)
  6. ...
展开查看全部

相关问题