按下按钮并打印后,单击鼠标更新打印,Python
你好强> 所以我得到了这个按钮,我点击来绘制一个图形,然后我希望这个图形随着每次鼠标的移动而更新,然后画出十字线 但我不能让它在按下按钮后更新图形,但如果我只是在课堂外绘图,效果会很好按下按钮并打印后,单击鼠标更新打印,Python,python,matplotlib,plot,widget,Python,Matplotlib,Plot,Widget,你好 所以我得到了这个按钮,我点击来绘制一个图形,然后我希望这个图形随着每次鼠标的移动而更新,然后画出十字线 但我不能让它在按下按钮后更新图形,但如果我只是在课堂外绘图,效果会很好 import cross_hair import numpy as np import matplotlib.pyplot as plt from PyQt5.QtWidgets import QApplication, QWidget, QPushButton 导入系统 class PushButton(QW
import cross_hair
import numpy as np
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
导入系统
class PushButton(QWidget):
def __init__(self):
super(PushButton,self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("PushButton")
self.setGeometry(400,400,300,260)
self.closeButton = QPushButton(self)
self.closeButton.setText("Press") #text
self.closeButton.clicked.connect(self.button_pressed)
def button_pressed(self):
#Fuction variables
self.x= np.arange(0, 1, 0.01)
self.y = np.sin(2 * 2 * np.pi * self.x)
#Figure
self.fig, self.ax = plt.subplots()
#title
self.ax.set_title('Snapping cursor')
#Plotting
self.line, = self.ax.plot(self.x, self.y, 'o')
#
snap_cursor = cross_hair.SnappingCursor(self.ax, self.line)
self.fig.canvas.mpl_connect('button_press_event', snap_cursor.on_mouse_click)
plt.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = PushButton()
ex.show()
sys.exit(app.exec_())
交叉头发代码看起来像这样(cross\u hair.py)
类捕捉光标:
def __init__(self, ax, line):
self.ax = ax
self.horizontal_line = ax.axhline(color='k', lw=0.8, ls='--')
self.vertical_line = ax.axvline(color='k', lw=0.8, ls='--')
self.x, self.y = line.get_data()
self._last_index = None
# text location in axes coords
self.text = ax.text(0.72, 0.9, '', transform=ax.transAxes)
def set_cross_hair_visible(self, visible):
need_redraw = self.horizontal_line.get_visible() != visible
self.horizontal_line.set_visible(visible)
self.vertical_line.set_visible(visible)
self.text.set_visible(visible)
return need_redraw
def on_mouse_click(self, event):
if not event.inaxes:
self._last_index = None
need_redraw = self.set_cross_hair_visible(False)
print('you no pressed' )#, event.button, event.xdata, event.ydata)
if need_redraw:
self.ax.figure.canvas.draw()
else:
self.set_cross_hair_visible(True)
x, y = event.xdata, event.ydata
print('you pressed') # , event.button, event.xdata, event.ydata)
index = min(np.searchsorted(self.x, x), len(self.x) - 1)
if index == self._last_index:
return # still on the same data point. Nothing to do.
self._last_index = index
x = self.x[index]
y = self.y[index]
# update the line positions
self.horizontal_line.set_ydata(y)
self.vertical_line.set_xdata(x)
self.text.set_text('x=%1.2f, y=%1.2f' % (x, y))
self.ax.figure.canvas.draw()
已解决 所以我尝试了一段时间来解决这个问题,现在用最简单的方法解决了。。。我想是吧 在类按钮中添加了函数update_plot(),如下所示
def update_plot(self):
print('Testing')
self.fig.canvas.mpl_connect('button_press_event', self.snap_cursor.on_mouse_click)
并在def按钮中添加了
self.update_plot()
plt.show()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update_plot)
self.timer.start(100)
在def中按下按钮
而ofc也被使用
from PyQt5 import QtCore
from PyQt5 import QtCore