pyqt5 / qtdesigner文本框默认标签

时间:2019-02-06 01:21:33

标签: python textbox pyqt5

我正在尝试为我的python程序创建GUI。我需要的工具之一是文本输入框。

现在,我想要此框的文字标签为“请插入文字”。是否具有添加标签的功能,该标签默认显示在输入文本框内,并在用户单击该框进行键入时消失?

我不介意使用qt设计器或pyqt5编码。

谢谢你们。

2 个答案:

答案 0 :(得分:1)

placeholderText : QString 此属性保存行编辑的占位符文本

import sys
from PyQt5.QtWidgets import QLineEdit, QVBoxLayout, QApplication, QWidget

class Test(QWidget):
    def __init__(self):
        super().__init__()

        self.lineEdit = QLineEdit(placeholderText="Please insert texts.")  # <---

        vbox = QVBoxLayout(self)
        vbox.addWidget(self.lineEdit)

if __name__ == '__main__':
    app  = QApplication(sys.argv)
    w = Test()
    w.show()
    sys.exit(app.exec_())

enter image description here

答案 1 :(得分:0)

我像你一样是乞gg,我的英语不太好。但是我建议您使用Qt Designer。绘制应用程序更容易,更快。我正在使用pyside2项目,建议您阅读要在PySide2项目和Qt Project中使用的每个小部件的docummentatio。尝试下面的代码

enter image description here

import sys
from PySide2.QtWidgets import QApplication
from PySide2.QtWidgets import QDialog
from PySide2.QtWidgets import QTextEdit
from PySide2.QtWidgets import QVBoxLayout
from PySide2.QtCore import Qt


class MainDialog(QDialog):
    def __init__(self, parent=None):
        super(MainDialog, self).__init__(parent)

        # Create Widget TextEdit
        self.text = QTextEdit()

        # I think that you wanna this function in your program
        # https://doc.qt.io/qtforpython/PySide2/QtWidgets/QLineEdit.html?highlight=qlineedit#PySide2.QtWidgets.PySide2.QtWidgets.QLineEdit.setPlaceholderText
        # http://doc.qt.io/qt-5/qlineedit.html#placeholderText-prop
        self.text.setPlaceholderText('''Yes! this is exactly what I want!
        Thank you, what if you have a big text box (more than 10 lines) and
        you want to scale up the place holder and align it in center?? ''')

        # https://doc.qt.io/qtforpython/PySide2/QtWidgets/QLineEdit.html?highlight=qlineedit#PySide2.QtWidgets.PySide2.QtWidgets.QLineEdit.setAlignment
        # http://doc.qt.io/qt-5/qlineedit.html#alignment-prop
        self.text.setAlignment(Qt.AlignCenter)

        # Layout
        layout = QVBoxLayout()
        layout.addWidget(self.text)
        self.setLayout(layout)


def main():

    app = QApplication()
    mainDialog = MainDialog()
    mainDialog.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
相关问题