这篇文章主要介绍了PyQt5怎么实现输入对话框的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇PyQt5怎么实现输入对话框文章都会有所收获,下面我们一起来看看吧。
输入对话框,用于弹窗获取用户的输入信息,包含输入列表,输入文本,输入数字等方式。
QInputDialog.getItem(self,"获取选项消息框", "名字列表", items),返回值Tuple[str, bool]
QInputDialog.getText(self,"获取文本消息框", "请输入文本信息:"),返回值Tuple[str, bool]
QInputDialog.getInt(self,"获取整数消息框", "请输入整数:"),返回值Tuple[int, bool]
QInputDialog.getMultiLineText(parent: QWidget, title: str, label: str, text: str = ..., flags: QtCore.Qt.WindowType = ..., inputMethodHints: QtCore.Qt.InputMethodHint = ...) -> typing.Tuple[str, bool]
QInputDialog.getDouble(parent: QWidget, title: str, label: str, value: float = ..., min: float = ..., max: float = ..., decimals: int = ..., flags: QtCore.Qt.WindowType = ..., step: float = ...) -> typing.Tuple[float, bool]
示例:
# _*_ coding:utf-8 _*_ import sys from PyQt6.QtWidgets import QApplication from PyQt6.QtWidgets import QWidget from PyQt6.QtWidgets import QMainWindow from PyQt6.QtWidgets import QFormLayout from PyQt6.QtWidgets import QPushButton from PyQt6.QtWidgets import QLineEdit from PyQt6.QtWidgets import QInputDialog from PyQt6.QtGui import QColor from PyQt6.QtGui import QIcon from PyQt6.QtCore import Qt class QInputDialogDemoView(QMainWindow): """输入消息框类""" def __init__(self): """构造函数""" super().__init__() self.setWindowTitle("MainWindow") self.setWindowIcon(QIcon(r"./res/20 (3).ico")) self.resize(200, 100) self.center() self.initui() def center(self): """居中显示""" win_rect = self.frameGeometry() # 获取窗口矩形 screen_center = self.screen().availableGeometry().center() # 屏幕中心 # 移动窗口矩形到屏幕中心 win_rect.moveCenter(screen_center) # 移动窗口与窗口矩形重合 self.move(win_rect.center()) def initui(self): """初始函数""" # 创建表单布局作为底层布局 self.formlayout = QFormLayout(self) self.formlayout.setAlignment(Qt.AlignmentFlag.AlignCenter) self.main_widget = QWidget() self.main_widget.setLayout(self.formlayout) self.setCentralWidget(self.main_widget) # 添加获取选项按钮 self.btn_getitem = QPushButton("Get Item") self.btn_getitem.clicked.connect(self.get_item) self.ledit_getitem = QLineEdit() self.formlayout.addRow(self.btn_getitem, self.ledit_getitem) # 添加获取文本按钮 self.btn_gettext = QPushButton("Get Text") self.btn_gettext.clicked.connect(self.get_text) self.ledit_gettext = QLineEdit() self.formlayout.addRow(self.btn_gettext, self.ledit_gettext) # 添加获取整数按钮 self.btn_getint = QPushButton("Get Int") self.btn_getint.clicked.connect(self.get_int) self.ledit_getint = QLineEdit() self.formlayout.addRow(self.btn_getint, self.ledit_getint) def get_item(self): """获取选项槽""" items = ("小张", "小明", "小李", "小朱") item,result = QInputDialog.getItem(self,"获取选项消息框", "名字列表", items) print(f"item : {item}, ok : {result}, tpye : {type(result)}") if item and result: self.ledit_getitem.setText(item) def get_text(self): """获取文本槽""" text,result = QInputDialog.getText(self,"获取文本消息框", "请输入文本信息:") print(f"item : {text}, ok : {result}, tpye : {type(result)}") if text and result: self.ledit_gettext.setText(text) def get_int(self): """获取文本槽""" num,result = QInputDialog.getInt(self,"获取整数消息框", "请输入整数:") print(f"item : {num}, ok : {result}, tpye : {type(result)}") if num and result: self.ledit_getint.setText(str(num)) if __name__ == "__main__": """主程序运行""" app = QApplication(sys.argv) window = QInputDialogDemoView() window.show() sys.exit(app.exec())
结果:
主界面:
输入选项:
输入文本:
输入整数:
关于“PyQt5怎么实现输入对话框”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“PyQt5怎么实现输入对话框”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。