PyQt5基础学习-QInputDialog(输入对话框) 1.QInputDialog.getItem(选择对话框) 2.QInputDialog.getText(文本输入框) 3.QInputDialog.getInt(数字输入框)
使用表单布局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)
每天更新pyQt5内容