PyQt5 QInputDialog(输入对话框) 学习

2020-04-04  本文已影响0人  _Mirage

QInputDialog provides a simple convenience dialog to get a single value from the user. The input value can be a string, a number, or an item from a list.

代码:

# coding = 'utf-8'

from PyQt5.QtWidgets import QWidget, QApplication,\
    QPushButton, QLineEdit, QInputDialog
import sys


class Gui(QWidget):
    def __init__(self):
        super(Gui, self).__init__()
        self.start()

    def start(self):
        # 创建按钮,作为类属性,是为了类内通信
        self.button = QPushButton('Dialog', self)
        self.button.move(20, 20)
        # 给按钮点击事件产生的信号连接到self.show_dialog这个槽
        self.button.clicked.connect(self.show_dialog)

        # 与前面的button异曲同工
        self.line_edit = QLineEdit(self)
        self.line_edit.move(115, 22)

        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('输入对话框')
        self.show()

    def show_dialog(self):
        # QInputDialog.getText参数:
        # getText(QWidget, str, str,\
        # echo: QLineEdit.EchoMode = QLineEdit.Normal,\
        # text: str = '', flags: Union[Qt.WindowFlags,\
        # Qt.WindowType] = Qt.WindowFlags(),\
        # inputMethodHints: Union[Qt.InputMethodHints,\
        # Qt.InputMethodHint] = Qt.ImhNone)\
        #   -> 返回值: Tuple[str, bool]
        name, flag = QInputDialog.getText(self, '输入对话框', '输入您的姓名:')
        # 如果用户点击QDialog的ok按钮,flag就是True
        if flag:
            self.line_edit.setText(name)


app = QApplication(sys.argv)
gui = Gui()
sys.exit(app.exec_())

运行结果: image.png image.png
上一篇下一篇

猜你喜欢

热点阅读