pycharm 如何阻止我的整个python程序关闭?

bxgwgixi  于 2022-11-08  发布在  PyCharm
关注(0)|答案(1)|浏览(242)

我是Python的新手,所以在做了简单的程序之后,我正在做一个程序。使用Tkinter,我成功地创建了主屏幕和登录屏幕(登录表单GUI)。但是,如果我不想登录,点击后退按钮,它应该关闭登录屏幕(登录表单GUI),但它关闭了整个程序。
我该如何避免这种情况?
下面是我创建应用程序的过程。

  1. class LoginFrame(ttk.Frame):
  2. def __init__(self, container):
  3. super().__init__(container)
  4. self.__createMain__()
  5. # Creates the widgets for the Main Screen
  6. def __createMain__(self):
  7. #Some Labels and entry buttons code
  8. Button2 = Button(self, text='Close', command=self.Closes).place(x=200, y=140)
  9. # Closes the main program
  10. def Closes(self):
  11. quit()
  12. class Login(tk.Tk):
  13. def __init__(self):
  14. super().__init__()
  15. self.title('Login Form')
  16. self.geometry('350x300')
  17. self.minsize(200,300)
  18. self.maxsize(500,500)
  19. self.__create_widgets()
  20. def __create_widgets(self):
  21. # create the input frame
  22. First_Frame = LoginFrame(self)
  23. First_Frame.place(height=200, width=700, x=20, y=20)

我正在通过一个命令调用Login类到我的主程序,它打开了,但是我不能关闭它。我该怎么做呢?

kmpatx3s

kmpatx3s1#

使用此代码。
这将关闭完整的登录屏幕。

  1. class LoginFrame(ttk.Frame):
  2. def __init__(self, container):
  3. self.con = container
  4. super().__init__(container)
  5. self.__createMain__()
  6. # Creates the widgets for the Main Screen
  7. def __createMain__(self):
  8. #Some Labels and entry buttons code
  9. Button2 = Button(self, text='Close', command=self.Closes).place(x=200, y=140)
  10. # Closes the main program
  11. def Closes(self):
  12. self.con.destroy()
  13. class Login(tk.Tk):
  14. def __init__(self):
  15. super().__init__()
  16. self.title('Login Form')
  17. self.geometry('350x300')
  18. self.minsize(200,300)
  19. self.maxsize(500,500)
  20. self.__create_widgets()
  21. def __create_widgets(self):
  22. # create the input frame
  23. First_Frame = LoginFrame(self)
  24. First_Frame.place(height=200, width=700, x=20, y=20)

但如果您要关闭LoginFrame Only,则将close函数更改为以下值。

  1. # Closes the main program
  2. def Closes(self):
  3. self.destroy()
展开查看全部

相关问题