pyqt5弹出对话框
时间: 2023-08-21 17:02:23 浏览: 177
pyqt弹出新对话框,以及关闭对话框获取数据的实例
在PyQt5中,弹出对话框是一种常见的交互方式。根据引用和引用的内容,有三种最常用的弹出窗口类型:输入框、消息提示框和文件夹对话框。其中,QInputDialog是用于弹出输入对话框的类,可以通过调用getInt()函数获取整数输入。示例代码如下:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog
import sys
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('PyQt5 弹出窗口')
self.resize(400, 300)
h1 = QHBoxLayout(self)
btn = QPushButton('弹出窗口')
btn.clicked.connect(self.showInputDialog)
h1.addWidget(btn)
def showInputDialog(self):
value, ok = QInputDialog.getInt(self, "输入框标题", "这是提示信息\n\n请输入整数:", 37, -10000, 10000, 2)
if ok:
print(f"输入的整数为:{value}")
else:
print("取消输入")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
```
以上代码展示了一个窗口,其中有一个按钮,当点击按钮时,会弹出一个输入对话框,要求用户输入一个整数。输入对话框的标题是"输入框标题",提示信息是"这是提示信息\n\n请输入整数:"。输入的整数会被打印出来。若用户点击取消按钮,则会打印"取消输入"。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [PyQt5弹出窗口--QInputDialog, QMessageBox, QFileDialog](https://blog.csdn.net/weixin_50113231/article/details/124102931)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* [Pyqt5 6种弹出对话框结合信号与槽使用](https://blog.csdn.net/maidu_xbd/article/details/85696510)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文