如何切换窗口python
原创如何切换窗口
在Python中,可以使用操作系统相关的模块来实现窗口的切换,常用的模块有tkinter
和pyqt
等。
使用tkinter
模块切换窗口的方法如下:
1、导入tkinter
模块
import tkinter as tk
2、创建两个窗口对象
window1 = tk.Tk() window2 = tk.Tk()
3、在第一个窗口中添加一个按钮,用于切换到第二个窗口
button = tk.Button(window1, text="切换到第二个窗口", command=lambda: window2.mainloop()) button.pack()
4、在第二个窗口中添加一个按钮,用于切换到第一个窗口
button = tk.Button(window2, text="切换到第一个窗口", command=lambda: window1.mainloop()) button.pack()
5、运行主循环,显示第一个窗口
window1.mainloop()
使用pyqt
模块切换窗口的方法如下:
1、导入pyqt
模块
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
2、创建两个窗口对象
class Window1(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() button = QPushButton("切换到第二个窗口", self) button.clicked.connect(self.switchToWindow2) layout.addWidget(button) self.setLayout(layout) self.setWindowTitle("第一个窗口") self.show() class Window2(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() button = QPushButton("切换到第一个窗口", self) button.clicked.connect(self.switchToWindow1) layout.addWidget(button) self.setLayout(layout) self.setWindowTitle("第二个窗口") self.show() app = QApplication([]) window1 = Window1() window2 = Window2()
上一篇:Python如何请求网页 下一篇:python如何验证偶数