Dialog
對話框是大多數現代GUI應用程式中不可缺少的一部分。對話是指兩個或兩個以上的人之間的對話。在計算機應用程式中,對話框是用來與應用程式“對話”的視窗。對話框用于輸入資料、修改資料、更改應用程式設定等。
QInputDialog
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.
QInputDialog提供簡單友善的對話框,從使用者那裡擷取一個值。輸入值可以是一個字元串、一個數字或一個清單中的項。
下面是源碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/10/24 下午3:18
# @Author : hukezhu
# @Site :
# @File : 1024-02-QInputDialog.py
# @Software: PyCharm
import sys
from PyQt5.QtWidgets import (QWidget,QPushButton,QLineEdit,QInputDialog,QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.btn = QPushButton('按鈕', self)
self.btn.move(20,20)
self.btn.clicked.connect(self.showDialog)
self.le = QLineEdit(self)
self.le.move(130,22)
self.setGeometry(300,300,390,150)
self.setWindowTitle('demo')
self.show()
def showDialog(self):
text, ok = QInputDialog.getText(self,'輸入對話框','輸入你的姓名:')
if ok:
self.le.setText(str(text))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
這個例子包含一個按鈕和一個輸入框,點選按鈕彈出該輸入框,在輸入框彙總輸入内容,并且點選确定之後,輸入的内容會顯示出來.
運作效果圖:

image.png