如何在程序运行时改变QPyLineEdit的内容?
How to change the content of QPyLineEdit when the program is running?
我正在用PyQt5写一个计算器,用户界面已经完成了,但是当我改变QLineEdit的内容时有一个问题。更改它的详细方法是在用户按下按钮时调用 QLineEdit.setText()
方法。
但是当我按下按钮时,程序结束并显示如下消息:"Process finished with exit code 1"。没有任何错误或警告信息。这是下面的完整程序:
# -*- coding:utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout,
QPushButton, QLineEdit, QVBoxLayout, QApplication, QMessageBox)
class Calculator(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# grid = QGridLayout()
qvbox = QVBoxLayout()
gridBar = QGridLayout()
gridWidget = QWidget()
numberEdit = QLineEdit()
numberEdit.setReadOnly(True)
names = ["Cls", "Bck", "", "Close",
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"]
position = [(i, j) for i in range(5) for j in range(4)]
for position, name in zip(position, names):
if name == "":
continue
button = QPushButton(name)
gridBar.addWidget(button, *position)
button.clicked.connect(self.buttonClicked)
qvbox.addWidget(numberEdit)
gridWidget.setLayout(gridBar)
qvbox.addWidget(gridWidget)
self.setLayout(qvbox)
self.move(300, 150)
self.setWindowTitle("Calculator")
self.show()
def buttonClicked(self):
sender = self.sender()
self.dealData(sender.text())
def dealData(self, data):
self.numberEdit.setText(data) # Here, the program exited!!!
print(data) # The output in consle was right.
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Calculator()
sys.exit(app.exec_())
我哪里做错了?
您必须为 numberEdit
设置一个属性以便稍后访问它:
class Calculator(QWidget):
...
def initUI(self):
...
self.numberEdit = QLineEdit()
self.numberEdit.setReadOnly(True)
此外,您需要追加新文本,因此dealData
应该如下所示:
def dealData(self, data):
self.numberEdit.end(False)
self.numberEdit.insert(data)
print(data) # The output in consle was right.
我正在用PyQt5写一个计算器,用户界面已经完成了,但是当我改变QLineEdit的内容时有一个问题。更改它的详细方法是在用户按下按钮时调用 QLineEdit.setText()
方法。
但是当我按下按钮时,程序结束并显示如下消息:"Process finished with exit code 1"。没有任何错误或警告信息。这是下面的完整程序:
# -*- coding:utf-8 -*-
import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout,
QPushButton, QLineEdit, QVBoxLayout, QApplication, QMessageBox)
class Calculator(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# grid = QGridLayout()
qvbox = QVBoxLayout()
gridBar = QGridLayout()
gridWidget = QWidget()
numberEdit = QLineEdit()
numberEdit.setReadOnly(True)
names = ["Cls", "Bck", "", "Close",
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"]
position = [(i, j) for i in range(5) for j in range(4)]
for position, name in zip(position, names):
if name == "":
continue
button = QPushButton(name)
gridBar.addWidget(button, *position)
button.clicked.connect(self.buttonClicked)
qvbox.addWidget(numberEdit)
gridWidget.setLayout(gridBar)
qvbox.addWidget(gridWidget)
self.setLayout(qvbox)
self.move(300, 150)
self.setWindowTitle("Calculator")
self.show()
def buttonClicked(self):
sender = self.sender()
self.dealData(sender.text())
def dealData(self, data):
self.numberEdit.setText(data) # Here, the program exited!!!
print(data) # The output in consle was right.
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Calculator()
sys.exit(app.exec_())
我哪里做错了?
您必须为 numberEdit
设置一个属性以便稍后访问它:
class Calculator(QWidget):
...
def initUI(self):
...
self.numberEdit = QLineEdit()
self.numberEdit.setReadOnly(True)
此外,您需要追加新文本,因此dealData
应该如下所示:
def dealData(self, data):
self.numberEdit.end(False)
self.numberEdit.insert(data)
print(data) # The output in consle was right.