PyQt的。如何将函数值转换为lineEdit?请帮助找到更好的方法

时间:2013-07-28 09:42:59

标签: python pyqt

我想创建一个简单的程序(在QtDesigner中),它在单击按钮时执行一个函数,并在LineEdit中显示该函数的值。 这是我的代码:

class MyForma1(object):
    def AddWidgets1(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(613, 545)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(244, 352, 111, 51))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(242, 290, 111, 20))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.retranslateUi(Form)

        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.changeText)

    def retranslateUi(self, Form):
        self.pushButton.setText(_translate("Form", "Click me", None))
        self.lineEdit.setText(_translate("Form", "functionvalue", None))

class MyForma2(QtGui.QDialog, MyForma1):
    def __init__(self, z11=0):
        QtGui.QDialog.__init__(self)
        self.AddWidgets1(self)
        self.z = z11

    def myfunc1(self):
        self.z+=1

    def changeText(self):
        self.myfunc1()
        self.lineEdit.setText(str(self.z))

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Forma = MyForma2()
    Forma.show()
    sys.exit(app.exec_())

实际上,它工作正常,但我不喜欢它的方式,我想让它更精致。这里的问题是按钮“执行”功能和值转换在一起。 我认为如果按钮只执行一个功能会更好,另外还有一些功能可以不断地将此功能的值从按钮转换为LineEdit 单独。例如,可能存在需要持续监视的功能值不仅受到按钮影响而且受某些其他事件影响的情况(例如:来自COM端口的输入信号)。在这种情况下,每次更改功能时发出信号都会很棒,不仅仅是按下按钮时。

1 个答案:

答案 0 :(得分:1)

查看此示例:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

#---------
# IMPORT
#---------
from PyQt4 import QtGui, QtCore

#---------
# MAIN
#---------
class MyWindow(QtGui.QWidget):
    valueChanged  = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.value = 0

        self.pushButtonAdd = QtGui.QPushButton(self)
        self.pushButtonAdd.setText("Add!")
        self.pushButtonAdd.clicked.connect(self.on_pushButtonAdd_clicked)

        self.pushButtonSubtract = QtGui.QPushButton(self)
        self.pushButtonSubtract.setText("Subtract!")
        self.pushButtonSubtract.clicked.connect(self.on_pushButtonSubtract_clicked)

        self.lineEditValue = QtGui.QLineEdit(self)
        self.lineEditValue.setText(str(self.value))

        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.pushButtonAdd)
        self.layoutVertical.addWidget(self.pushButtonSubtract)
        self.layoutVertical.addWidget(self.lineEditValue)

    @QtCore.pyqtSlot()
    def on_pushButtonAdd_clicked(self):
        self.valueChanged.emit(1)

    @QtCore.pyqtSlot()
    def on_pushButtonSubtract_clicked(self):
        self.valueChanged.emit(-1)

    def setValue(self, value):
        self.value += value
        self.lineEditValue.setText(str(self.value))

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.resize(333, 111)
    main.valueChanged.connect(main.setValue)
    main.show()

    sys.exit(app.exec_())

在此示例中,要手动更改值,您可以调用main.setValue(34)