QinputDialog

输入的值可以是字符串,数字,或者一个项目从一个列表

def showDialog(self):
    text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
    if ok:
        self.le.setText(str(text))

# 显示对话框;第一个字符串是对话框标题,第二个字符串是对话框中的消息
# 对话框返回一个输入文本和一个布尔值,点击OK按钮,布尔值是True

# 对话框中收到的消息会显示在行编辑文本框中

 1 import sys
 2 from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit, QInputDialog, QApplication)
 3 class Example(QWidget):
 4     def __init__(self):
 5         super().__init__()
 6         self.initUI()
 7 
 8     def initUI(self):
 9         self.btn = QPushButton('Dialog', self)  # 创建一个Dialog的按钮
10         self.btn.move(20, 20)
11         self.btn.clicked.connect(self.showDialog)   # btn信号连接到self.showDialog槽
12 
13         self.le = QLineEdit(self)   # 行编辑实例
14         self.le.move(130, 22)
15 
16         self.setGeometry(300, 300, 290, 150)
17         self.setWindowTitle('Input dialog')
18         self.show()
19 
20     def showDialog(self):
21         # 显示对话框;第一个字符串是对话框标题,第二个字符串是对话框中的消息
22         # 对话框返回一个输入文本和一个布尔值,点击OK按钮,布尔值是True
23         text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
24         if ok:
25             self.le.setText(str(text))
26 
27 if __name__ == '__main__':
28     app = QApplication(sys.argv)
29     ex = Example()
30     sys.exit(app.exec_())
QinputDialog

相关文章:

  • 2021-06-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2021-07-17
  • 2021-07-24
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-29
  • 2022-12-23
  • 2021-10-30
  • 2022-12-23
相关资源
相似解决方案