Python Pyside2 暂停脚本,直到用户从滑块或 qlineedit 返回值



当满足某些条件时,是否可以暂停python脚本,以便用户可以通过弹出窗口(最好是pyside2滑块或qlineedit(输入输入,然后在用户给出值后恢复脚本。

我唯一能找到的是qMessageBox问题,但我唯一可以输入的选项是2个按钮,在这种情况下没有用。

任何帮助将不胜感激。

谢谢!!

您可以使用 QDialog。 http://pyside.github.io/docs/pyside/PySide/QtGui/QDialog.html

https://wiki.qt.io/Qt_for_Python_Tutorial_SimpleDialog

执行以下操作。

from PySide import QtGui  # from PySide2 import QtWidgets or from qtpy import QtWidgets
dialog = QtGui.QDialog()
lay = QtGui.QFormLayout()
dialog.setLayout(lay)
slider = QtGui.QSlider()
lay.addRow(QtGui.QLabel('Slider'), slider)
... # Accept buttons
ans = dialog.exec_()  # This will block until the dialog closes
# Check if dialog was accepted?
value = slider.value()
... # Continue code.

与exec_QMessageBox类似的是这个示例。 https://gist.github.com/tcrowson/8152683242018378a00b

您可能可以使用 QMessageBox 并设置布局以更改外观。

这是怎么回事?

Essential PySide 通过运行事件循环来工作。它运行这个无限的 while 循环,将事件从队列中取出并处理它们。任何鼠标移动或按钮单击都是一个事件。

app = QApplication([])
app.exec_()  # This is running the event loop until the application closes.
print('here')  # This won't print until the application closes

您可以使用任何小部件手动重现此内容。

app = QApplication([])  # Required may be automatic with IPython
slider = QSlider()  # No Parent
slider.show()
# Slider is not visible until the application processes the slider.show() event
app.processEvents()
while slider.isVisible():  # When user clicks the X on the slider it will hide the slider
app.processEvents()  # Process events like the mouse moving the slider
print('here')  # This won't print until the Slider closes
... # Continue code script

最新更新