M. *_*son 2 pyqt click button save
我在 PyQT 中创建单击按钮时遇到了一些麻烦。当我创建如下按钮时,此图片无法保存
cv.SetImageROI(image, (pt1[0],pt1[1],pt2[0] - pt1[0],int((pt2[1] - pt1[1]) * 1)))
if self.Button.click():
cv.SaveImage('example.jpg', image)
cv.ResetImageROI(image)
Run Code Online (Sandbox Code Playgroud)
您的代码中的问题是您正在QPushButton.click对在线调用的按钮执行编程单击if self.Button.click():,您需要做的是将信号连接QPushButton.clicked到代码上的适当插槽。Singals 和 Slots 是 Qt 处理可能发生在对象上的一些重要事件的方式。给大家举个例子,希望对大家有帮助:
import PyQt4.QtGui as gui
#handler for the signal aka slot
def onClick(checked):
print checked #<- only used if the button is checkeable
print 'clicked'
app = gui.QApplication([])
button = gui.QPushButton()
button.setText('Click me!')
#connect the signal 'clicked' to the slot 'onClick'
button.clicked.connect(onClick)
#perform a programmatic click
button.click()
button.show()
app.exec_()
Run Code Online (Sandbox Code Playgroud)
注意:要了解底层行为,请阅读 Qt/PyQt 的文档。