- PyQt 教程
- PyQt - 首页
- PyQt - 简介
- PyQt - 环境配置
- PyQt - Hello World
- PyQt - 主要类
- PyQt - 使用Qt Designer
- PyQt - 元对象
- PyQt 信号与槽
- PyQt - 信号和槽
- PyQt - 支持和信号
- PyQt - 未绑定和已绑定信号
- PyQt - 使用PyQtSignal创建新的信号
- PyQt - 连接、断开和发射信号
- PyQt - 槽装饰器
- PyQt - 槽连接
- PyQt 布局
- PyQt - 布局管理
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt 基本部件
- PyQt - 基本部件
- PyQt - QLabel部件
- PyQt - QLineEdit部件
- PyQt - QPushButton部件
- PyQt - QRadioButton部件
- PyQt - QCheckBox部件
- PyQt - QComboBox部件
- PyQt - QSpinBox部件
- PyQt - QMessageBox
- PyQt - QDialogButtonBox部件
- PyQt - QFontComboBox部件
- PyQt - QDoubleSpinBox部件
- PyQt - QToolBox部件
- PyQt - QDialog类
- PyQt - QMessageBox
- PyQt - 多文档界面
- PyQt - 拖放
- PyQt 绘图API
- PyQt - 绘图API
- PyQt 数据库
- PyQt - 数据库处理
- PyQt 核心知识
- PyQt - BrushStyle 常量
- PyQt - QClipboard
- PyQt - QPixmap类
- PyQt 有用资源
- PyQt - 快速指南
- PyQt - 有用资源
- PyQt - 讨论
PyQt - QInputDialog部件
这是一个预配置的对话框,带有一个文本字段和两个按钮,“确定”和“取消”。当用户点击“确定”按钮或按下 Enter 键后,父窗口会收集文本框中的输入。
用户输入可以是数字、字符串或列表中的项目。还会显示一个提示用户应该做什么的标签。
QInputDialog 类具有以下静态方法来接受用户的输入:
| 序号 | 方法及描述 |
|---|---|
| 1 |
getInt() 创建一个用于输入整数的微调框。 |
| 2 |
getDouble() 可以输入浮点数的微调框。 |
| 3 |
getText() 一个简单的行编辑字段用于输入文本。 |
| 4 |
getItem() 一个组合框,用户可以从中选择项目。 |
示例
以下示例实现了输入对话框功能。顶级窗口有三个按钮。它们的 clicked() 信号通过连接的槽弹出 InputDialog。
items = ("C", "C++", "Java", "Python")
item, ok = QInputDialog.getItem(self, "select input dialog",
"list of languages", items, 0, False)
if ok and item:
self.le.setText(item)
def gettext(self):
text, ok = QInputDialog.getText(self, 'Text Input Dialog', 'Enter your name:')
if ok:
self.le1.setText(str(text))
def getint(self):
num,ok = QInputDialog.getInt(self,"integer input dualog","enter a number")
if ok:
self.le2.setText(str(num))
完整的代码如下:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class inputdialogdemo(QWidget):
def __init__(self, parent = None):
super(inputdialogdemo, self).__init__(parent)
layout = QFormLayout()
self.btn = QPushButton("Choose from list")
self.btn.clicked.connect(self.getItem)
self.le = QLineEdit()
layout.addRow(self.btn,self.le)
self.btn1 = QPushButton("get name")
self.btn1.clicked.connect(self.gettext)
self.le1 = QLineEdit()
layout.addRow(self.btn1,self.le1)
self.btn2 = QPushButton("Enter an integer")
self.btn2.clicked.connect(self.getint)
self.le2 = QLineEdit()
layout.addRow(self.btn2,self.le2)
self.setLayout(layout)
self.setWindowTitle("Input Dialog demo")
def getItem(self):
items = ("C", "C++", "Java", "Python")
item, ok = QInputDialog.getItem(self, "select input dialog",
"list of languages", items, 0, False)
if ok and item:
self.le.setText(item)
def gettext(self):
text, ok = QInputDialog.getText(self, 'Text Input Dialog', 'Enter your name:')
if ok:
self.le1.setText(str(text))
def getint(self):
num,ok = QInputDialog.getInt(self,"integer input dualog","enter a number")
if ok:
self.le2.setText(str(num))
def main():
app = QApplication(sys.argv)
ex = inputdialogdemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
以上代码产生以下输出:
广告