qt点击A界面的pushbutton按钮弹出B窗口,在B窗口点击B窗口的pushbutton后在A界面textedit中显示数字
时间: 2023-12-05 07:03:19 浏览: 50
以下为Python代码实现:
```python
# 导入PyQt5模块
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QTextEdit
# 定义A界面
class A(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 添加一个pushbutton按钮
self.btn = QPushButton('打开B窗口', self)
self.btn.move(50, 50)
self.btn.clicked.connect(self.showB)
# 添加一个textedit文本框
self.textedit = QTextEdit(self)
self.textedit.setGeometry(50, 100, 200, 150)
# 设置A界面的大小和标题
self.setGeometry(300, 300, 300, 300)
self.setWindowTitle('A界面')
def showB(self):
# 打开B窗口
self.b = B(self)
self.b.show()
# 在A界面的textedit中显示数字
def setText(self, text):
self.textedit.setText(text)
# 定义B界面
class B(QWidget):
def __init__(self, parent):
super().__init__()
self.parent = parent
self.initUI()
def initUI(self):
# 添加一个pushbutton按钮
self.btn = QPushButton('在A界面显示数字', self)
self.btn.move(50, 50)
self.btn.clicked.connect(self.showNum)
# 设置B界面的大小和标题
self.setGeometry(350, 350, 200, 100)
self.setWindowTitle('B界面')
# 在A界面的textedit中显示数字
def showNum(self):
self.parent.setText('123')
if __name__ == '__main__':
# 创建应用程序对象
app = QApplication(sys.argv)
# 创建A界面对象
a = A()
# 显示A界面
a.show()
# 进入应用程序的主循环,并通过sys.exit函数确保主循环安全结束
sys.exit(app.exec_())
```
运行程序,点击A界面的pushbutton按钮弹出B窗口,在B窗口点击B窗口的pushbutton后在A界面textedit中显示数字"123"。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![dll](https://img-home.csdnimg.cn/images/20210720083646.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)