QinputDialog提供了一種獲取用戶單值數據的簡介形式。python
它接受的數據有字符串、數字、列表中的一項數據app
# QInputDialog 輸入對話框 # 本示例包含一個按鈕和一個行編輯部件。單擊按鈕會彈出對話框,以獲取用戶輸入的文本數據。該文本數據將會顯示在行編輯部件中。 import sys from PyQt4 import QtCore, QtGui class MainWindow(QtGui.QWidget): def __init__(self, parent = None): QtGui.QWidget.__init__(self, parent) self.setGeometry(300, 300, 350, 80) self.setWindowTitle('InputDialog') self.button = QtGui.QPushButton('Dialog', self) self.button.setFocusPolicy(QtCore.Qt.NoFocus) self.button.move(20, 20) self.connect(self.button, QtCore.SIGNAL('clicked()'), self.showDialog) self.setFocus() self.label = QtGui.QLineEdit(self) self.label.move(130, 22) def showDialog(self): text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:') # 該語句用來顯示一個輸入對話框。第一個參數"Input Dialog"是對話框的標題, 第二個參數"Enter your name"將做爲提示信息顯示在對話框內。 # 該對話框將返回用戶輸入的內容和一個布爾值。若是用戶單擊OK按鈕確認輸入,則返回的布爾值是true,不然返回的布爾值爲false。 if ok: self.label.setText(unicode(text)) app = QtGui.QApplication(sys.argv) main = MainWindow() main.show() sys.exit(app.exec_())