简体   繁体   English

Python PyQt在表单之间发送数据

[英]Python PyQt Send Data Between Forms

I have two forms built with Qt Designer. 我有两种使用Qt Designer构建的表单。 The two forms utilize the uic.loadUiType process described here . 这两种形式利用此处描述的uic.loadUiType过程。 I have uploaded the UI forms to this location To import the forms. 我已将UI表单上载到此位置以导入表单。 The Main windows has three push buttons. 主窗口有三个按钮。 When each button is pushed – attempting to pass which button was passed to the Number Pad Form – trying to use signals and slots but not working. 按下每个按钮时-尝试将哪个按钮传递给数字键盘表单-尝试使用信号和插槽,但不起作用。

When the NumPad form opens I need it to populate with Field1, 2, or 3 so that I can pass the contents of txtDatToPass back to the Main Window Form. 当NumPad窗体打开时,我需要它填充Field1、2或3,以便我可以将txtDatToPass的内容传递回主窗口窗体。 Not sure why the signals and not getting through. 不知道为什么信号并没有通过。 Any thoughts or guidance would be helpful Thanks 任何想法或指导会有所帮助,谢谢

import sys
from PyQt5 import QtWidgets, QtCore 
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QApplication, QLabel
from PyQt5 import uic


qtCreatorFile = "MainWinForm.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

qtCreatorFileKeyPad = "NumPadForm.ui"
Ui_KeyPad, QtBaseClass = uic.loadUiType(qtCreatorFileKeyPad )


class PunchWindow(QtWidgets.QMainWindow):
    signalPassDataToMainForm = QtCore.pyqtSignal(str,str)
    def __init__(self):
        super(PunchWindow, self).__init__()
        self.ui = Ui_KeyPad()
        self.ui.setupUi(self)
        self.move(850, 200) #Center Screen

  #NumberPad
        self.ui.btnOne.clicked.connect(lambda: self.numberPad(1))
        self.ui.btnTwo.clicked.connect(lambda: self.numberPad(2))
        self.ui.btnThree.clicked.connect(lambda: self.numberPad(3))
        self.ui.btnFour.clicked.connect(lambda: self.numberPad(4))
        self.ui.btnFive.clicked.connect(lambda: self.numberPad(5))
        self.ui.btnSix.clicked.connect(lambda: self.numberPad(6))
        self.ui.btnSeven.clicked.connect(lambda: self.numberPad(7))
        self.ui.btnEight.clicked.connect(lambda: self.numberPad(8))
        self.ui.btnNine.clicked.connect(lambda: self.numberPad(9))
        self.ui.btnZero.clicked.connect(lambda: self.numberPad(0))
        self.ui.btnDot.clicked.connect(lambda: self.numberPad("."))
        self.ui.btnBackSpace.clicked.connect(lambda: self.numberPad("BS"))
        self.ui.btnClear.clicked.connect(lambda: self.numberPad("Clear"))
        self.ui.btnEnter.clicked.connect(self.Enter)
##
    def numberPad(self, n):
        print(n)
        strField = self.ui.txtDataField.toPlainText()
        if(strField == "Field1") or (strField == "Field2") or (strField == "Field3"):
            strValue = self.ui.txtDataToPass.toPlainText()
            strN = str(n)
            if(strN == "BS"):
                strTrim = strValue[:-1]
                self.ui.txtDataToPass.setText(strTrim)
            elif(strN == "Clear"):
                self.ui.txtDataToPass.setText("")
            else:        
                strValue = strValue + strN 
                self.ui.txtDataToPass.setText(strValue)

    def Enter(self):
        strFieldNo = self.ui.txtDataField.toPlainText()
        strSendData = self.ui.txtDataToPass.toPlainText()
        print("Trying to send contents of txtDataToPass back to MainWindow Form -- Data Field:  " + strFieldNo +  "      Data: "  + strSendData)
        self.signalPassDataToMainForm.emit(strFieldNo, strSendData)



class MainWindow(QtWidgets.QMainWindow):
    signalPassData = QtCore.pyqtSignal(str) # used to send Field1, 2 , or 3 to Punch Window

    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()        
        self.ui.setupUi(self)
        self.move(450, 200) #Center Screen

        self.ui.btnField1.clicked.connect(self.Field1)
        self.ui.btnField2.clicked.connect(self.Field2)
        self.ui.btnField3.clicked.connect(self.Field3)

        #Should recieve signals back from PunchWindow
        self.Punch = PunchWindow()
        self.Punch.signalPassDataToMainForm.connect(self.Update)

    def Update(self, strField, strData):
        self.ui.txtData1.setText(strData)

    def Field1(self):
        strField = "Field1"
        print(strField)
        self.ui.SW = PunchWindow()
        self.ui.SW.show()
        self.signalPassData.emit(strField)

    def Field2(self):
        strField = "Field2"
        print(strField)
        self.ui.SW = PunchWindow()
        self.ui.SW.show()
        self.signalPassData.emit(strField)

    def Field3(self):
        strField = "Field3"
        print(strField)
        self.ui.SW = PunchWindow()
        self.ui.SW.show()
        self.signalPassData.emit(strField)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MW = MainWindow()
    MW.show()
    sys.exit(app.exec_())

The first thing is to design the classes, in the case of PunchWindow you must have a method to update the txtDataField and have a signal that sends the data to MainWindow. 首先是设计类,在PunchWindow的情况下,您必须具有一种方法来更新txtDataField并具有将数据发送到MainWindow的信号。 On the other hand in MainWindow every time you press on the buttons you must update the txtDataField using the method that was mentioned initially, then it is in the slot connected to signalPassDataToMainForm you must distinguish the appropriate field. 另一方面,在MainWindow中,每次按下按钮时,都必须使用最初提到的方法更新txtDataField,然后必须在连接到signalPassDataToMainForm的插槽中区分适当的字段。

import sys
from functools import partial
from PyQt5 import QtCore, QtWidgets, uic

qtCreatorFile = "MainWinForm.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

qtCreatorFileKeyPad = "NumPadForm.ui"
Ui_KeyPad, QtBaseClass = uic.loadUiType(qtCreatorFileKeyPad )

class PunchWindow(QtWidgets.QMainWindow, Ui_KeyPad):
    signalPassDataToMainForm = QtCore.pyqtSignal(str, str)

    def __init__(self, parent=None):
        super(PunchWindow, self).__init__(parent)
        self.setupUi(self)
        self.move(850, 200) #Center Screen
        buttons = (self.btnOne, self.btnTwo, self.btnThree, self.btnFour,
            self.btnFive, self.btnSix, self.btnSeven, self.btnEight,
            self.btnNine, self.btnZero, self.btnDot, self.btnBackSpace,
            self.btnClear)
        vals = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0, ".", "BS", "Clear")
        for button, val in zip(buttons, vals):
            button.clicked.connect(partial(self.numberPad, val))
        self.btnEnter.clicked.connect(self.enter)

    @QtCore.pyqtSlot(str)
    def setCurrentField(self, text):
        self.txtDataField.setPlainText(text)

    def numberPad(self, n):
        strField = self.txtDataField.toPlainText()
        if strField in ("Field1", "Field2", "Field3"):
            strValue = self.txtDataToPass.toPlainText()
            strN = str(n)
            if strN == "BS":
                self.txtDataToPass.setPlainText(strValue[:-1])
            elif strN == "Clear":
                self.txtDataToPass.clear()
            else:        
                strValue += strN 
                self.txtDataToPass.setPlainText(strValue)

    @QtCore.pyqtSlot()
    def enter(self):
        strFieldNo = self.txtDataField.toPlainText()
        strSendData = self.txtDataToPass.toPlainText()
        self.signalPassDataToMainForm.emit(strFieldNo, strSendData)

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.move(450, 200) #Center Screen
        buttons = (self.btnField1, self.btnField2, self.btnField3)
        fields = ("Field1", "Field2", "Field3")
        for button, field in zip(buttons, fields):
            button.clicked.connect(partial(self.updatePunch, field))
        self.punch = PunchWindow()
        self.punch.signalPassDataToMainForm.connect(self.updateField)

    @QtCore.pyqtSlot(str, str)
    def updateField(self, strField, strData):
        if strField == "Field1":
            self.txtData1.setText(strData)
        elif strField == "Field2":
            self.txtData2.setText(strData)
        elif strField == "Field3":
            self.txtData3.setText(strData)

    @QtCore.pyqtSlot()
    def updatePunch(self, field):
        self.punch.setCurrentField(field)
        self.punch.show()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MW = MainWindow()
    MW.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM