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

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

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=&quot;PyQt5 Simple Application&quot;
        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(&quot;Calculator!&quot;)

        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(&quot;%s = &lt;b&gt;%s&lt;/b&gt;&quot; %(text, eval(text)))
        except:
            self.browser.append(
                &quot;&lt;font - color = red&gt; &lt;b&gt;%s&lt;/b&gt; is invalid! Type number only &lt;/font&gt;&quot; %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

可以将多个处理程序连接到一个事件,如 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:

确定