tkinter登录成功跳转主窗体
时间: 2023-07-05 07:27:16 浏览: 61
好的,对于这个问题,我可以给您提供一个大致的解决方案。首先,您需要在登录窗体中添加一个“登录”按钮,并在按钮的点击事件中进行登录验证。如果登录验证通过,则可以使用`Toplevel`创建一个新的主窗体,并关闭登录窗体。下面是一个简单的示例代码:
```python
import tkinter as tk
class LoginWindow(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('300x200')
self.title('登录')
self.username_label = tk.Label(self, text='用户名:')
self.username_label.pack()
self.username_entry = tk.Entry(self)
self.username_entry.pack()
self.password_label = tk.Label(self, text='密码:')
self.password_label.pack()
self.password_entry = tk.Entry(self, show='*')
self.password_entry.pack()
self.login_button = tk.Button(self, text='登录', command=self.login)
self.login_button.pack()
def login(self):
# 这里进行登录验证,如果验证通过,则打开主窗体
if self.username_entry.get() == 'admin' and self.password_entry.get() == 'password':
self.destroy() # 关闭登录窗体
MainWindow()
class MainWindow(tk.Toplevel):
def __init__(self):
super().__init__()
self.geometry('400x300')
self.title('主窗体')
self.label = tk.Label(self, text='欢迎使用主窗体!')
self.label.pack()
if __name__ == '__main__':
login_window = LoginWindow()
login_window.mainloop()
```
在上述代码中,我们首先创建了一个`LoginWindow`类,继承自`tk.Tk`,用于创建登录窗体。在登录窗体中,我们添加了用户名和密码的输入框以及一个“登录”按钮。当用户点击“登录”按钮时,会触发`login`方法进行登录验证。如果登录验证通过,则会关闭登录窗体,并打开一个新的`MainWindow`窗体。
在`MainWindow`窗体中,我们添加了一个标签,用于显示欢迎信息。注意,我们使用了`Toplevel`来创建新窗体,而不是使用`Tk`,因为一个应用程序只需要一个`Tk`实例。
最后,在`if __name__ == '__main__'`中,我们创建了一个`LoginWindow`实例,并启动主循环。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rp](https://img-home.csdnimg.cn/images/20210720083646.png)