对话框——输入对话框

 1 # !/usr/bin/python3
 2 # -*- coding: utf-8 -*-
 3 
 4 """
 5 ZetCode PyQt5 tutorial
 6 
 7 In this example, we receive data from
 8 a QInputDialog dialog.
 9 
10 Aauthor: Jan Bodnar
11 Website: zetcode.com
12 Last edited: August 2017
13 """
14 
15 from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit, QInputDialog, QApplication)
16 import sys
17 
18 
19 class Example(QWidget):
20 
21     def __init__(self):
22         super().__init__()
23 
24         self.initUI()
25 
26     def initUI(self):
27 
28         self.btn = QPushButton('Dialog', self)
29         self.btn.move(20, 20)
30         self.btn.clicked.connect(self.showDialog)
31 
32         self.le = QLineEdit(self)
33         self.le.move(130, 22)
34 
35         self.setGeometry(300, 300, 290, 150)
36         self.setWindowTitle('Input dialog')
37         self.show()
38 
39     def showDialog(self):
40 
41         # The dialog returns the entered text and a boolean value.
42         # if we click the Ok button, the boolean value is true
43         # The first string is a dialog title, the second one is a message within the dialog
44         text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
45 
46         if ok:
47             # The text that we have received from the dialog is 
48             # set to the line edit widget with setText()
49             self.le.setText(str(text))
50 
51 
52 if __name__ == '__main__':
53 
54     app = QApplication(sys.argv)
55     ex = Example()
56     sys.exit(app.exec_())
原文地址:https://www.cnblogs.com/fuqia/p/8734758.html