通过点击按钮实现两个窗口进行切换
创建第一个窗口
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/46c9dec17c7c4e8f83aa118c444109a4.png#pic_center)
class window_1(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Window 1')
self.resize(400,300)
self.button1 = QPushButton('Button1',self)
self.button1.move(100,50)
创建第二个窗口
class window_2(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Window 2')
self.resize(400,300)
self.button2 = QPushButton('Button2',self)
self.button2.move(100,50)
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/fc2778c534534d31bbc83cb9261fe49a.png#pic_center)
窗口切换
class switcher:
def __init__(self, window_1,window_2):
self.window1 = window_1
self.window2 = window_2
self.window1.button1.clicked.connect(self.switch_to_window2)
self.window2.button2.clicked.connect(self.switch_to_window1)
def switch_to_window2(self):
self.window1.hide()
self.window2.show()
def switch_to_window1(self):
self.window2.hide()
self.window1.show()
完整代码
import sys
from PySide6.QtWidgets import QApplication, QPushButton,QWidget
class window_1(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Window 1')
self.resize(400,300)
self.button1 = QPushButton('Button1',self)
self.button1.move(100,50)
class window_2(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Window 2')
self.resize(400,300)
self.button2 = QPushButton('Button2',self)
self.button2.move(100,50)
class switcher:
def __init__(self, window_1,window_2):
self.window1 = window_1
self.window2 = window_2
self.window1.button1.clicked.connect(self.switch_to_window2)
self.window2.button2.clicked.connect(self.switch_to_window1)
def switch_to_window2(self):
self.window1.hide()
self.window2.show()
def switch_to_window1(self):
self.window2.hide()
self.window1.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window_1 = window_1()
window_2 = window_2()
s = switcher(window_1,window_2)
window_1.show()
sys.exit(app.exec())
更多示例
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton
class Window1(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.resize(400, 300)
self.setWindowTitle('Window1')
self.button1 = QPushButton('Button 1', self)
self.button1.move(150,100)
self.button1.clicked.connect(self.switch_to_window2)
def switch_to_window2(self):
self.hide()
self.s = Window2()
self.s.show()
class Window2(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.resize(400, 300)
self.setWindowTitle('Window2')
self.button2 = QPushButton('Button 2', self)
self.button2.move(120,100)
self.button2.clicked.connect(self.switch_to_window1)
def switch_to_window1(self):
self.hide()
self.f = Window1()
self.f.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Window1()
w.show()
sys.exit(app.exec())
多子窗口切换
from PySide6.QtWidgets import QApplication, QMainWindow, QMdiArea, QMdiSubWindow, QWidget
from PySide6.QtCore import Qt
import sys
from ui_main import Ui_MainWindow
from ui_gui1 import Ui_gui1
from ui_gui2 import Ui_gui2
class gui1(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_gui1()
self.ui.setupUi(self)
class gui2(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_gui2()
self.ui.setupUi(self)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.add_subwindow()
self.btn_clicked()
def add_subwindow(self):
self.sub_window1 = self.ui.mdiArea.addSubWindow(gui1())
self.sub_window1.setWindowFlags(Qt.FramelessWindowHint)
self.sub_window1.showMaximized()
self.sub_window2 = self.ui.mdiArea.addSubWindow(gui2())
self.sub_window2.setWindowFlags(Qt.FramelessWindowHint)
self.sub_window2.hide()
def btn_clicked(self):
self.ui.pushButton.clicked.connect(lambda: self.switch_window(self.sub_window1))
self.ui.pushButton_2.clicked.connect(lambda: self.switch_window(self.sub_window2))
def switch_window(self,sub_window):
for sub in self.ui.mdiArea.subWindowList():
sub.hide()
sub_window.showMaximized()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()