我想创建一个简单的程序(在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端口的输入信号)。而且在这种情况下,每次更改功能时都发出信号是一件好事,不仅是在按下按钮时。

最佳答案

看看这个例子:

#!/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)

关于python - PyQt。如何将函数值转换为lineEdit?请帮助寻找更好的方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17906920/

10-13 04:10