鱼C论坛

 找回密码
 立即注册
查看: 3202|回复: 2

PyQt5两个窗体之间信息传递问题

[复制链接]
发表于 2017-3-23 00:31:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from PyQt5.QtCore import pyqtSlot
  2. from PyQt5.QtWidgets import QDialog

  3. from PyQt5 import QtCore, QtGui, QtWidgets

  4. class Ui_Mainwindow(object):
  5.     def setupUi(self, Mainwindow):
  6.         Mainwindow.setObjectName("Mainwindow")
  7.         Mainwindow.resize(400, 300)
  8.         Mainwindow.setSizeGripEnabled(True)
  9.         self.textBrowser = QtWidgets.QTextBrowser(Mainwindow)
  10.         self.textBrowser.setGeometry(QtCore.QRect(60, 70, 256, 51))
  11.         self.textBrowser.setObjectName("textBrowser")
  12.         self.label = QtWidgets.QLabel(Mainwindow)
  13.         self.label.setGeometry(QtCore.QRect(60, 40, 251, 16))
  14.         self.label.setObjectName("label")
  15.         self.pushButton = QtWidgets.QPushButton(Mainwindow)
  16.         self.pushButton.setGeometry(QtCore.QRect(140, 170, 93, 28))
  17.         self.pushButton.setObjectName("pushButton")

  18.         self.retranslateUi(Mainwindow)
  19.         QtCore.QMetaObject.connectSlotsByName(Mainwindow)

  20.     def retranslateUi(self, Mainwindow):
  21.         _translate = QtCore.QCoreApplication.translate
  22.         Mainwindow.setWindowTitle(_translate("Mainwindow", "主窗体"))
  23.         self.label.setText(_translate("Mainwindow", "TextLabel"))
  24.         self.pushButton.setText(_translate("Mainwindow", "打开对话框"))

  25. class Ui_Secondwin(object):
  26.     def setupUi(self, Secondwin):
  27.         Secondwin.setObjectName("Secondwin")
  28.         Secondwin.resize(400, 300)
  29.         Secondwin.setSizeGripEnabled(True)
  30.         self.lineEdit = QtWidgets.QLineEdit(Secondwin)
  31.         self.lineEdit.setGeometry(QtCore.QRect(100, 90, 151, 21))
  32.         self.lineEdit.setObjectName("lineEdit")
  33.         self.pushButton = QtWidgets.QPushButton(Secondwin)
  34.         self.pushButton.setGeometry(QtCore.QRect(130, 160, 93, 28))
  35.         self.pushButton.setObjectName("pushButton")

  36.         self.retranslateUi(Secondwin)
  37.         self.pushButton.clicked.connect(Secondwin.close)
  38.         QtCore.QMetaObject.connectSlotsByName(Secondwin)

  39.     def retranslateUi(self, Secondwin):
  40.         _translate = QtCore.QCoreApplication.translate
  41.         Secondwin.setWindowTitle(_translate("Secondwin", "副窗体"))
  42.         self.pushButton.setText(_translate("Secondwin", "确定"))



  43. class Secondwin(QDialog, Ui_Secondwin):
  44.     """
  45.     Class documentation goes here.
  46.     """
  47.     def __init__(self, parent=None):
  48.         """
  49.         Constructor
  50.         
  51.         @param parent reference to the parent widget
  52.         @type QWidget
  53.         """
  54.         super(Secondwin, self).__init__(parent)
  55.         self.setupUi(self)
  56.    
  57.     @pyqtSlot()
  58.     def on_pushButton_clicked(self):
  59.         """
  60.         Slot documentation goes here.
  61.         """
  62.         my_str=self.lineEdit.text()
  63. ##########################################问题就在这
  64. #我想通过调用函数让副窗口的字符串传给主窗口的textBrowser,但是不知道怎么写,我写了一个,错了,静态方法没法用self
  65. #        Mainwindow.chuandi(my_str, self)
  66.         Mainwindow.textBrowser.append(my_str)
  67.         
  68.         self.close()
  69.         


  70. class Mainwindow(QDialog, Ui_Mainwindow):
  71.     """
  72.     Class documentation goes here.
  73.     """
  74.     def __init__(self, parent=None):
  75.         """
  76.         Constructor
  77.         
  78.         @param parent reference to the parent widget
  79.         @type QWidget
  80.         """
  81.         super(Mainwindow, self).__init__(parent)
  82.         self.setupUi(self)
  83.         
  84.     def chuandi(str, self):
  85.         self.textBrowser.append(str)
  86.         
  87.    
  88.     @pyqtSlot()
  89.     def on_pushButton_clicked(self):
  90.         """
  91.         Slot documentation goes here.
  92.         """
  93.         secondwin = Secondwin()
  94.         secondwin.exec_()
  95.       



  96. if __name__ == "__main__":
  97.     import sys
  98.     from PyQt5.QtWidgets import QApplication
  99.     app = QApplication(sys.argv)
  100.     dlg = Mainwindow()
  101.     dlg.show()
  102.     sys.exit(app.exec_())
  103.    
复制代码

我想通过调用函数让副窗口的字符串传给主窗口的textBrowser,但是不知道怎么写,我写了一个,错了.
我使用Mainmin.textBrowser,它告诉没有这个属性,我搞不懂了
有谁会的,或者会信号量与槽的,求帮助
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-10 17:37:29 | 显示全部楼层
我也有这样的问题,楼主解决了吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-10 21:48:06 | 显示全部楼层
https://fishc.com.cn/thread-131082-1-1.html  我用pyqt写的,用到信号和槽,不知道能不能帮到你,我自己不是很熟悉,参照视频写的,有需要可以下载代码自己看下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 07:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表