PyQt5 里的LineEdit控件如何使用按钮追加文本
刚学PyQt5,准备自己写一个计算器,但是按钮每次按下,文本框内的数字都会被更新掉,怎么才能追加写入文本呢代码如下import sys
from PyQt5.QtWidgets import QWidget,QGridLayout,QApplication,QPushButton,QLineEdit
from PyQt5.QtGui import QFont,QIcon
class Calculator(QWidget):
def __init__(self):
super(Calculator, self).__init__()
self.initUI()
def initUI(self):
"""创建网格布局类"""
grid = QGridLayout(self)
self.setLayout(grid)
"""创建按钮位置"""
locs = [(y,x) for y in range(1,5) for x in range(4)]
"""创建按钮名称"""
btn_name = ["7","8","9","+","4","5","6","-","1","2","3","*","0",".","+","/"]
for name,loc in zip(btn_name,locs):
button = QPushButton(name)
button.clicked.connect(self.enterNum)
button.setFont(self.btnfont())
grid.addWidget(button,*loc)
"""创建输入框"""
self.enterLine = QLineEdit()
self.enterLine.setFont(self.btnfont())
grid.addWidget(self.enterLine,0,0)
self.setWindowTitle("Calculator")
self.setGeometry(300,300,300,300)
self.show()
def btnfont(self):
return QFont("Microsoft YaHei",12)
"""按下数字函数"""
def enterNum(self):
strNum = ''#保存输入内容
sender = self.sender()
strNum = strNum + sender.text()
print(strNum)
self.enterLine.setText(strNum)
if __name__ == '__main__':
app = QApplication(sys.argv)
cal = Calculator()
sys.exit(app.exec_())
求个大佬解答{:10_266:} 先获取lineEdit的文本,对获取的文本进行追加,然后再重新设置文本。
"""按下数字函数"""
def enterNum(self):
strNum = self.enterLine.text()#保存输入内容
sender = self.sender()
strNum = strNum + sender.text()
print(strNum)
self.enterLine.setText(strNum)
由于用手机打字的,你的代码我没有细看,可能搞错,但是你明白我的意思就好了{:10_266:}{:10_266:} hrp 发表于 2020-4-15 00:59
先获取lineEdit的文本,对获取的文本进行追加,然后再重新设置文本。
由于用手机打字的,你的代码我没有 ...
解决了,感谢大佬{:10_266:}
页:
[1]