1.
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSignal
import sys
"""
自定义对话框
"""
class MyDialog(QDialog):
# 自定义信号
mySignal = pyqtSignal(str)
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
self.initUI()
def initUI(self):
self.edit = QLineEdit(self)
self.edit.move(10, 10)
button = QPushButton('发送', self)
button.move(10, 40)
button.clicked.connect(self.sendEditContent)
self.setWindowTitle('MyDialog')
self.setGeometry(300, 300, 300, 200)
def sendEditContent(self):
content = self.edit.text()
self.mySignal.emit(content) # 发射信号
"""
主窗口
"""
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.initUI()
def initUI(self):
self.button = QPushButton('open', self)
self.button.clicked.connect(self.openMyDialog)
self.button.move(10, 10)
self.label = QLabel("hello", self)
self.label.move(10, 50)
self.setWindowTitle('Window')
self.setGeometry(300, 300, 300, 200)
self.show()
def openMyDialog(self):
my = MyDialog(self)
# 在主窗口中连接信号和槽
my.mySignal.connect(self.getDialogSignal)
my.exec_()
"""
实现槽函数
"""
def getDialogSignal(self, connect):
self.label.setText(connect)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
w.show()
print('父类窗口大小'+str(w.pos()))
sys.exit(app.exec_())
2.
t3.py
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSignal
import sys
from t4 import t4
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.initUI()
def initUI(self):
self.button = QPushButton('open', self)
self.button.clicked.connect(self.new)
self.setWindowTitle('Window')
self.resize(200,200)
def new(self):
my = t4(self)
# 在主窗口中连接信号和槽
my.mySignal.connect(self.setText)
my.exec_()
# 实现槽函数
def setText(self, connect):
self.button.setText(connect)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
w.show()
print('父类窗口大小'+str(w.pos()))
sys.exit(app.exec_())
t4.py
from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSignal
import sys
"""
自定义对话框
"""
class t4(QDialog):
# 自定义信号
mySignal = pyqtSignal(str)
def __init__(self, parent=None):
super(t4, self).__init__(parent)
self.initUI()
def initUI(self):
button = QPushButton('发送', self)
button.move(10, 40)
button.clicked.connect(self.sendText)
self.setWindowTitle('MyDialog')
self.setGeometry(300, 300, 300, 200)
def sendText(self):
self.mySignal.emit('test2') # 发射信号
if __name__ == "__main__":
app = QApplication(sys.argv)
w = t4()
w.show()
print('父类窗口大小'+str(w.pos()))
sys.exit(app.exec_())