将字符串发送到QLineEdit

时间:2014-10-26 20:35:57

标签: python pyqt4 signals-slots qlineedit

我想知道是否有办法让按钮向PyQt4中的lineEdit变量发送消息。到目前为止,这是我的代码:

import sys
from PyQt4 import QtGui,QtCore

class Example(QtGui.QWidget):     
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        qanda = QtGui.QLineEdit()
        one = QtGui.QPushButton('1',self)
        two = QtGui.QPushButton('2',self)
        three = QtGui.QPushButton('3',self)
        four = QtGui.QPushButton('4',self)
        five = QtGui.QPushButton('5',self)
        six = QtGui.QPushButton('6',self)
        seven = QtGui.QPushButton('7',self)
        eight = QtGui.QPushButton('8',self)
        nine = QtGui.QPushButton('9',self)
        zero = QtGui.QPushButton('0',self)
        dot = QtGui.QPushButton('.',self)
        minus = QtGui.QPushButton('-',self)
        plus = QtGui.QPushButton('+',self)
        multiply = QtGui.QPushButton('x',self)
        divide = QtGui.QPushButton('/',self)
        equals = QtGui.QPushButton('=',self)
        backspace = QtGui.QPushButton('Backspace',self)
        xsquared = QtGui.QPushButton('x^2',self)
        xcubed = QtGui.QPushButton('x^3',self)
        clear = QtGui.QPushButton('C',self)

        one.clicked.connect(self.buttonClicked)
        two.clicked.connect(self.buttonClicked)
        three.clicked.connect(self.buttonClicked)
        four.clicked.connect(self.buttonClicked)
        five.clicked.connect(self.buttonClicked)
        six.clicked.connect(self.buttonClicked)
        seven.clicked.connect(self.buttonClicked)
        eight.clicked.connect(self.buttonClicked)
        nine.clicked.connect(self.buttonClicked)
        zero.clicked.connect(self.buttonClicked)
        dot.clicked.connect(self.buttonClicked)
        minus.clicked.connect(self.buttonClicked)
        plus.clicked.connect(self.buttonClicked)
        multiply.clicked.connect(self.buttonClicked)
        divide.clicked.connect(self.buttonClicked)
        equals.clicked.connect(self.buttonClicked)
        backspace.clicked.connect(self.buttonClicked)
        xsquared.clicked.connect(self.buttonClicked)
        xcubed.clicked.connect(self.buttonClicked)
        clear.clicked.connect(self.buttonClicked)

        grid = QtGui.QGridLayout()
        grid.setSpacing(5)

        grid.addWidget(qanda,0,0,1,4)
        grid.addWidget(one,4,0)
        grid.addWidget(two,4,1)
        grid.addWidget(three,4,2)
        grid.addWidget(four,3,0)
        grid.addWidget(five,3,1)
        grid.addWidget(six,3,2)
        grid.addWidget(seven,2,0)
        grid.addWidget(eight,2,1)
        grid.addWidget(nine,2,2)
        grid.addWidget(zero,5,0)
        grid.addWidget(dot,5,1)
        grid.addWidget(minus,5,3)
        grid.addWidget(plus,4,3)
        grid.addWidget(multiply,2,3)
        grid.addWidget(divide,3,3)
        grid.addWidget(equals,5,2)
        grid.addWidget(backspace,1,0)
        grid.addWidget(xsquared,1,1)
        grid.addWidget(xcubed,1,2)

        grid.addWidget(clear,1,3)

        self.setLayout(grid) 

        self.setGeometry(300, 300, 250, 200)
        self.setWindowTitle('ldm;sasdklhsdghk dgh jkl;')    
        self.show()

def buttonClicked(self):
    sender = self.sender()
    print(sender.text())

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

我的目标是当有人点击按钮时,例如x ^ 2,x ^ 2将显示在变量qanda的空白区域。

如果您想知道,这是一个计算器。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您需要保留对行编辑的引用,以便以后可以访问它:

    def initUI(self):
        self.qanda = QtGui.QLineEdit(self)
        ...
        grid.addWidget(self.qanda,0,0,1,4)
        ...

现在您可以设置行编辑的文本,如下所示:

    def buttonClicked(self):
        sender = self.sender()
        print(sender.text())
        self.qanda.setText(sender.text())