如何将QInputDialog设置为模式
问题描述:
我试图从QtGui使用对话来获取用户的一些输入。 对于QFileDialog,它可以像我预期的那样工作,但是当我使用QInputDialog时,会弹出对话框并继续执行代码,而无需等待用户输入。 下面是一个简单的例子:如何将QInputDialog设置为模式
from PyQt4 import QtGui
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from numpy import pi
class Canvas(FigureCanvas):
def __init__(self):
self.fig = Figure()
FigureCanvas.__init__(self, self.fig)
self.fig.canvas.mpl_connect('key_press_event',self.key_pressed)
self.fig.canvas.mpl_connect('button_press_event',self.on_left_click)
self.ax = self.fig.add_axes([0,0,1,1])
self.figure.canvas.show()
def key_pressed(self, event):
if event.key == 'f':
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
'c:\\',"Image files (*.png *.jpg *.gif)")
print fname
def on_left_click(self,event):
# If the mouse pointer is not on the canvas, ignore buttons
if not event.inaxes: return
if event.button==1:
x=event.xdata
y=event.ydata
r, ok = QtGui.QInputDialog.getDouble(self, 'Text Input Dialog', 'Enter radius:', 10)
if ok:
self.ax.scatter(x, y, s=pi*r**2,c=0.5)
self.draw()
cnv = Canvas()
答
我试着用PySide而不是PyQt4的复制本。你的代码只是打开画布小部件,然后立即关闭它并退出。这意味着应用程序事件循环未正常运行。
尝试改变你的最后一行:
app = QtGui.QApplication([])
cnv = Canvas()
app.exec_()
有了这个一切正常。
现在有用,谢谢。 (我在Spyder内部开始它的开放。) – Tomas