python tkinter 切换窗口教程
时间: 2023-07-02 22:04:55 浏览: 188
以下是一个简单的 Python Tkinter 切换窗口的教程:
首先,我们需要导入 Tkinter 库。
```python
import tkinter as tk
```
接下来,我们需要创建一个主窗口和一个子窗口。我们可以使用 `Toplevel()` 函数来创建一个新的顶级窗口。
```python
# 创建主窗口
root = tk.Tk()
# 创建子窗口
window = tk.Toplevel(root)
```
接下来,我们需要将子窗口隐藏起来,并将它们绑定到按钮上。
```python
def open_window():
window.deiconify()
def close_window():
window.withdraw()
# 创建打开子窗口的按钮
open_button = tk.Button(root, text="Open Window", command=open_window)
# 创建关闭子窗口的按钮
close_button = tk.Button(window, text="Close Window", command=close_window)
```
最后,我们需要运行主循环。
```python
root.mainloop()
```
完整的代码如下:
```python
import tkinter as tk
def open_window():
window.deiconify()
def close_window():
window.withdraw()
# 创建主窗口
root = tk.Tk()
# 创建子窗口
window = tk.Toplevel(root)
window.withdraw()
# 创建打开子窗口的按钮
open_button = tk.Button(root, text="Open Window", command=open_window)
open_button.pack()
# 创建关闭子窗口的按钮
close_button = tk.Button(window, text="Close Window", command=close_window)
close_button.pack()
# 运行主循环
root.mainloop()
```
当你点击 "Open Window" 按钮时,子窗口将显示出来。当你点击 "Close Window" 按钮时,子窗口将被隐藏。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)