最近在研究qt5,研究到多窗口切换,发现网上讲的不是很清楚,在此记录一下
首先创建页面
first = pyqtSignal()
为一个连接槽,用于连接QPushButton
的事件
self.first.emit()
发出一个信号
class FirstUI(QWidget):
first = pyqtSignal()
def __init__(self, *args, **kwargs):
super(FirstUI, self).__init__(*args, **kwargs)
self.resize(500, 500)
self.but = QPushButton('first', self)
self.but.resize(100, 40)
self.but.move(100, 100)
self.but.clicked.connect(self.but_clicked)
def but_clicked(self):
self.first.emit()
self.close()
emit()
默认为空,如果添加值,例:emit('first ')时pyqtSignal()
必须要一个参数接收数据pyqtSignal(str)
str为数据类型,可自己替换
需要注意的是emit('first ')
与pyqtSignal(str)
数据类型要统一
可以这么写:
class FirstUI(QWidget):
first = pyqtSignal(str)
def __init__(self, *args, **kwargs):
super(FirstUI, self).__init__(*args, **kwargs)
self.resize(500, 500)
self.but = QPushButton('first', self)
self.but.resize(100, 40)
self.but.move(100, 100)
self.but.clicked.connect(self.but_clicked)
def but_clicked(self):
self.first.emit('first')
self.close()
代码与第一页基本相同
第一种写法
class SecondUi(QWidget):
second = pyqtSignal()
def __init__(self, *args, **kwargs):
super(SecondUi, self).__init__(*args, **kwargs)
self.resize(500, 500)
self.but = QPushButton('second', self)
self.but.resize(100, 40)
self.but.move(100, 100)
self.but.clicked.connect(self.but_clicked)
def but_clicked(self):
self.second.emit()
self.close()
第二种写法
class SecondUi(QWidget):
second = pyqtSignal(str)
def __init__(self, *args, **kwargs):
super(SecondUi, self).__init__(*args, **kwargs)
self.resize(500, 500)
self.but = QPushButton('second', self)
self.but.resize(100, 40)
self.but.move(100, 100)
self.but.clicked.connect(self.but_clicked)
def but_clicked(self):
self.second.emit('second')
self.close()
self.firstui.first.connect(self.second)
重写FirstUI()
中的QPushButton
事件
同理SecondUi()
也是
注:重写后,程序会先运行
def but_clicked(self): self.second.emit('second') self.close()
函数,运行完毕后会继续续运行自定义的函数
class MainWindow:
def first(self):
self.firstui = FirstUI()
self.firstui.first.connect(self.second)
self.firstui.show()
def second(self):
self.secondui = SecondUi()
self.secondui.second.connect(self.first)
self.secondui.show()
如果需要接收
self.second.emit('second')
中的’second’值,在调用槽函数时,在self.firstui.first.connect(self.second)
中 可用self.second
接收
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
class FirstUI(QWidget):
first = pyqtSignal(str)
def __init__(self, *args, **kwargs):
super(FirstUI, self).__init__(*args, **kwargs)
self.resize(500, 500)
self.but = QPushButton('first', self)
self.but.resize(100, 40)
self.but.move(100, 100)
self.but.clicked.connect(self.but_clicked)
def but_clicked(self):
self.first.emit('first')
self.close()
class SecondUi(QWidget):
second = pyqtSignal(str)
def __init__(self, *args, **kwargs):
super(SecondUi, self).__init__(*args, **kwargs)
self.resize(500, 500)
self.but = QPushButton('second', self)
self.but.resize(100, 40)
self.but.move(100, 100)
self.but.clicked.connect(self.but_clicked)
def but_clicked(self):
self.second.emit('second')
self.close()
class MainWindow:
def first(self):
self.firstui = FirstUI()
self.firstui.first.connect(self.second)
self.firstui.show()
def second(self, st):
print(st)
self.secondui = SecondUi()
self.secondui.second.connect(self.first)
self.secondui.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ma = MainWindow()
ma.first()
sys.exit(app.exec_()) # app.exet_()是指程序一直循环运行直到主窗口被关闭终止进程(如果没有这句话,程序运行时会一闪而过)
若不需要第三个调用函数
MainWindow()
,也可以这么写
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
class FirstUI(QWidget):
def __init__(self, *args, **kwargs):
super(FirstUI, self).__init__(*args, **kwargs)
self.resize(500, 500)
self.but = QPushButton('first', self)
self.but.resize(100, 40)
self.but.move(100, 100)
self.but.clicked.connect(self.but_clicked)
def but_clicked(self):
self.close()
# SecondUi必须为类的一个属性,否则程序会一闪而过,必须带self.
self.s = SecondUi()
self.s.show()
class SecondUi(QWidget):
def __init__(self, *args, **kwargs):
super(SecondUi, self).__init__(*args, **kwargs)
self.resize(500, 500)
self.but = QPushButton('second', self)
self.but.resize(100, 40)
self.but.move(100, 100)
self.but.clicked.connect(self.but_clicked)
def but_clicked(self):
self.close()
# FirstUI必须为类的一个属性,否则程序会一闪而过,必须带self.
self.f = FirstUI()
self.f.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
f = FirstUI()
f.show()
sys.exit(app.exec_())