英文:
How to clear QLineEdit default text when I click?
问题
I just want to simply ask how to clear my default text for QLineEdit with mouse click event?
只想简单询问如何使用鼠标点击事件来清除QLineEdit的默认文本?
Also want to ask how to clear text box when every time I type a number in the text box and press enter?
还想问一下,每次我在文本框中输入数字并按回车键时,如何清除文本框的内容?
My code is below;
我的代码如下;
from PyQt5.QtWidgets import QApplication, QTextBrowser, QLineEdit, QVBoxLayout, QWidget
from PyQt5.QtCore import pyqtSignal
import sys
class Window(QWidget):
def init(self):
super().init()
self.title = "PyQt5 Simple Application"
self.top = 400
self.left = 400
self.width = 800
self.height = 600
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self height)
self.Ui()
def Ui(self):
self.browser = QTextBrowser()
self.lineEdit = QLineEdit("Calculator!")
vbox = QVBoxLayout()
vbox.addWidget(self.browser)
vbox.addWidget(self.lineEdit)
self.setLayout(vbox)
self.lineEdit.returnPressed.connect(self.updateBrowser)
def updateBrowser(self):
try:
text = str(self.lineEdit.text())
self.browser.append("%s = <b>%s</b>" % (text, eval(text)))
except:
self.browser.append("<font color=red><b>%s</b> is invalid! Type number only </font>" % text)
app = QApplication(sys.argv)
window = Window()
window.show()
app.exec()
Thanks in advance.
提前感谢。
英文:
I just want to simply ask how to clear my default text for QLineEdit with mouse click event?
Also want to ask how to clear text box when everytime I type number in text box and press enter?
My code is below;
from PyQt5.QtWidgets import QApplication, QTextBrowser, QLineEdit, QVBoxLayout, QWidget
from PyQt5.QtCore import pyqtSignal
import sys
class Window(QWidget) :
def __init__ (self):
super().__init__()
self.title="PyQt5 Simple Application"
self.top = 400
self.left = 400
self.width = 800
self.height = 600
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.Ui()
def Ui (self):
self.browser = QTextBrowser()
self.lineEdit = QLineEdit("Calculator!")
vbox = QVBoxLayout()
vbox.addWidget(self.browser)
vbox.addWidget(self.lineEdit)
self.setLayout(vbox)
self.lineEdit.returnPressed.connect(self.updateBrowser)
def updateBrowser(self):
try:
text = str(self.lineEdit.text ())
self.browser.append("%s = <b>%s</b>" %(text, eval(text)))
except:
self.browser.append(
"<font - color = red> <b>%s</b> is invalid! Type number only </font>" %text
)
app = QApplication(sys.argv)
window = Window()
window.show()
app.exec()
Thanks in advance
答案1
得分: 1
你可以将一个 lambda 函数绑定到 clicked
事件上:self.lineEdit.clicked.connect(lambda x: self.lineEdit.setText("")
你也可以用相同的方式处理 self.lineEdit.returnPressed
。
可以将多个处理程序连接到一个事件,如 returnPressed
或 clicked
。
英文:
You can bind a lambda function to a clicked
event:
self.lineEdit.clicked.connect(lambda x: self.lineEdit.setText(""))
You can do the same with self.lineEdit.returnPressed
.
Multiple handlers can be connected to one event like returnPressed
or clicked
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论