使用表單布局FormLayer, 通過點擊按鈕,綁定對話框,點擊完按鈕對話框彈出
QInputDialogDemo.py
""" 輸入對話框: QInputDialog QInputDialog.getItem QInputDialog.getText QInputDialog.getInt """ import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt class QInputDialogDemo(QWidget): def __init__(self): super(QInputDialogDemo, self).__init__() self.initUI() def initUI(self): self.setWindowTitle("輸入對話框") #表單對話框 layout = QFormLayout() self.button1 = QPushButton("獲取列表中的選項") self.button1.clicked.connect(self.getItem) self.lineEdit1 = QLineEdit() self.lineEdit1.setDisabled(True) layout.addRow(self.button1, self.lineEdit1) self.button2 = QPushButton("獲取字符串") self.button2.clicked.connect(self.getText) self.lineEdit2 = QLineEdit() self.lineEdit2.setDisabled(True) layout.addRow(self.button2, self.lineEdit2) self.button3 = QPushButton("獲取整數") self.button3.clicked.connect(self.getInt) self.lineEdit3 = QLineEdit() self.lineEdit3.setDisabled(True) layout.addRow(self.button3, self.lineEdit3) self.setLayout(layout) def getItem(self): items = ('C', "C++", 'Ruby', 'Python', 'Java') # 構造選項對話框 item, ok = QInputDialog.getItem(self, "請選擇編程語言", "語言列表", items) # 如果有選擇, 就顯示在文本框中 if ok and item: self.lineEdit1.setText(item) def getText(self): # 構造文本對話框 text, ok = QInputDialog.getText(self, "文本輸入框", "輸入姓名") # 如果有輸入就顯示在文本框中 if ok and text: self.lineEdit2.setText(text) def getInt(self): # 構造數字對話框 num, ok = QInputDialog.getInt(self, "整數輸入框", '輸入數字') # 如果有輸入就顯示在文本框中 if ok and num: self.lineEdit3.setText(str(num)) if __name__ == "__main__": app = QApplication(sys.argv) main = QInputDialogDemo() main.show() sys.exit(app.exec_())
主界面
選擇對話框(QInputDialog.getItem)
輸入對話框(QInputDialog.getText)
、
數字對話框(QInputDialog.getInt)