我需要PYTHON的多窗口UI代码
时间: 2024-03-01 16:50:14 浏览: 17
以下是使用Python的Tkinter模块实现多窗口UI的示例代码:
```python
import tkinter as tk
class Window1:
def __init__(self, parent):
self.parent = parent
self.frame = tk.Frame(parent)
self.frame.pack()
self.text = tk.Label(self.frame, text="This is Window 1")
self.text.pack()
self.button = tk.Button(self.frame, text="Open Window 2", command=self.open_window2)
self.button.pack()
def open_window2(self):
self.window2 = Window2(self.parent)
self.window2.frame.tkraise()
class Window2:
def __init__(self, parent):
self.parent = parent
self.frame = tk.Frame(parent)
self.frame.pack()
self.text = tk.Label(self.frame, text="This is Window 2")
self.text.pack()
self.button = tk.Button(self.frame, text="Open Window 1", command=self.open_window1)
self.button.pack()
def open_window1(self):
self.window1 = Window1(self.parent)
self.window1.frame.tkraise()
if __name__ == "__main__":
root = tk.Tk()
root.geometry("200x100")
window1 = Window1(root)
root.mainloop()
```
该代码定义了两个窗口类`Window1`和`Window2`,分别用于创建两个不同的窗口。在每个窗口中,我们定义了一个按钮,用于在两个窗口之间切换。当用户单击按钮时,我们调用相应的方法来打开另一个窗口。在这个示例中,我们使用`tkraise()`方法来在窗口之间切换。
可以根据自己的需要修改窗口大小、按钮文本等。希望这个示例代码可以帮助你实现多窗口UI。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)