如何通过使用mousepressEvent打开新的小部件

时间:2018-10-05 12:42:33

标签: python pyqt4 mayavi

在这里,我单击任意位置我想打开一个新的小部件时,通过使用鼠标按下事件来固定x,y位置?在该小部件中,我想创建3d图,请问我可以如何解决此问题 如下是我的代码:

 def mousePressEvent(self, evnt):
            print evnt
            print dir(evnt)
            print evnt.screenPos().x()
            print evnt.screenPos().y()


            self.ix = int(evnt.screenPos().x()/Setting.WIDTH)
            self.iy = int(evnt.screenPos().y()/Setting.HEIGHT)
            print self.ix , self.iy
            #self.widget = QtGui.QWidget()
            #self.widget.show()


import scipy as np
from mayavi import mlab


x,y = np.mgrid[-3:3:100j, -3:3:100j]

a = 0.3

a0 = 0.2

r1=np.sqrt((x-a)**2 + y**2)

values = np.sinc(-r1/a0)



mlab.surf(values, warp_scale='auto')

mlab.outline()

mlab.axes()
# mlab.test_contour3d()
mlab.show()

1 个答案:

答案 0 :(得分:0)

import sys

from PyQt4.QtGui import QWidget, QApplication

class MyWidget(QWidget):
    def mousePressEvent(self, evnt):
        print evnt
        print dir(evnt)
        print evnt.screenPos().x()
        print evnt.screenPos().y()


        self.ix = int(evnt.screenPos().x()/Setting.WIDTH)
        self.iy = int(evnt.screenPos().y()/Setting.HEIGHT)
        print self.ix , self.iy
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
app.exec_()