如何在单击时清除QLineEdit的默认文本?

huangapple go评论90阅读模式
英文:

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()

  1. self.title = "PyQt5 Simple Application"
  2. self.top = 400
  3. self.left = 400
  4. self.width = 800
  5. self.height = 600
  6. self.setWindowTitle(self.title)
  7. self.setGeometry(self.top, self.left, self.width, self height)
  8. self.Ui()
  9. def Ui(self):
  10. self.browser = QTextBrowser()
  11. self.lineEdit = QLineEdit("Calculator!")
  12. vbox = QVBoxLayout()
  13. vbox.addWidget(self.browser)
  14. vbox.addWidget(self.lineEdit)
  15. self.setLayout(vbox)
  16. self.lineEdit.returnPressed.connect(self.updateBrowser)
  17. def updateBrowser(self):
  18. try:
  19. text = str(self.lineEdit.text())
  20. self.browser.append("%s = <b>%s</b>" % (text, eval(text)))
  21. except:
  22. 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;

  1. from PyQt5.QtWidgets import QApplication, QTextBrowser, QLineEdit, QVBoxLayout, QWidget
  2. from PyQt5.QtCore import pyqtSignal
  3. import sys
  4. class Window(QWidget) :
  5. def __init__ (self):
  6. super().__init__()
  7. self.title=&quot;PyQt5 Simple Application&quot;
  8. self.top = 400
  9. self.left = 400
  10. self.width = 800
  11. self.height = 600
  12. self.setWindowTitle(self.title)
  13. self.setGeometry(self.top, self.left, self.width, self.height)
  14. self.Ui()
  15. def Ui (self):
  16. self.browser = QTextBrowser()
  17. self.lineEdit = QLineEdit(&quot;Calculator!&quot;)
  18. vbox = QVBoxLayout()
  19. vbox.addWidget(self.browser)
  20. vbox.addWidget(self.lineEdit)
  21. self.setLayout(vbox)
  22. self.lineEdit.returnPressed.connect(self.updateBrowser)
  23. def updateBrowser(self):
  24. try:
  25. text = str(self.lineEdit.text ())
  26. self.browser.append(&quot;%s = &lt;b&gt;%s&lt;/b&gt;&quot; %(text, eval(text)))
  27. except:
  28. self.browser.append(
  29. &quot;&lt;font - color = red&gt; &lt;b&gt;%s&lt;/b&gt; is invalid! Type number only &lt;/font&gt;&quot; %text
  30. )
  31. app = QApplication(sys.argv)
  32. window = Window()
  33. window.show()
  34. app.exec()

Thanks in advance

答案1

得分: 1

你可以将一个 lambda 函数绑定到 clicked 事件上:self.lineEdit.clicked.connect(lambda x: self.lineEdit.setText("")

你也可以用相同的方式处理 self.lineEdit.returnPressed

可以将多个处理程序连接到一个事件,如 returnPressedclicked

英文:

You can bind a lambda function to a clicked event:
self.lineEdit.clicked.connect(lambda x: self.lineEdit.setText(&quot;&quot;))

You can do the same with self.lineEdit.returnPressed.

Multiple handlers can be connected to one event like returnPressed or clicked.

huangapple
  • 本文由 发表于 2023年2月23日 20:58:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定