我是PyQtGraph的新手,想要用它来快速可视化我的数据采集.以前我使用的是matplotlib,重绘图形是我的瓶颈.在转换到PyQtGraph之后,我目前只缺少matplotlib的一个功能.即,返回鼠标光标的x和y坐标.
如何在使用PyQtGraph绘制的图中调用/模仿鼠标光标的x坐标和y坐标的返回?
编辑! - 在实现leongold的提示后,代码能够返回鼠标位置而不会失去速度.代码如下:
import numpy
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
def gaussian(A, B, x):
return A * numpy.exp(-(x/(2. * B))**2.)
def mouseMoved(evt):
mousePoint = p.vb.mapSceneToView(evt[0])
label.setText("<span style='font-size: 14pt; color: white'> x = %0.2f, <span style='color: white'> y = %0.2f</span>" % (mousePoint.x(), mousePoint.y()))
# Initial data frame
x = numpy.linspace(-5., 5., 10000)
y = gaussian(5., 0.2, x)
# Generate layout
win = pg.GraphicsWindow()
label = pg.LabelItem(justify = "right")
win.addItem(label)
p = win.addPlot(row = 1, col …Run Code Online (Sandbox Code Playgroud) 我的假设是,即使一个函数什么也不做,它仍然应该占用内存,但是在下面,foo如果Foo有一个额外的函数,为什么不占用更多的内存呢?
class Foo():
def __init__(self):
pass
def why_does_this_not_use_memory(self):
pass
class Bar():
def __init__(self):
pass
import sys
foo = Foo()
print(sys.getsizeof(foo)) # 56
bar = Bar()
print(sys.getsizeof(bar)) # 56
Run Code Online (Sandbox Code Playgroud) 在这段代码中,我按住 'shift' 将屏幕变为绿色。如果在按住 'shift' 时 pygame 焦点被中断,它会跳过 KEYUP 事件,并且 pygame 继续认为正在按住 'shift'。模拟 KEYUP 事件不起作用。我发现的唯一解决方法是手动按下和释放“shift”,但我不希望用户必须这样做。
为了演示,运行代码并按住“Shift”,然后按住“Enter”打开一个对话框。然后松开“Shift”,然后退出对话框。即使没有按住“Shift”键,绿屏也会保留。
如果在将 'embedding_pygame_and_showing_the_bug' 设置为 False 后再次运行代码,您将看到 KEYUP 事件没有被跳过。
import tkinter as tk
import pygame
import os
from tkinter.simpledialog import askstring
root = tk.Tk()
root.geometry("200x100")
embedding_pygame_and_showing_the_bug = True
if embedding_pygame_and_showing_the_bug:
embed_frame = tk.Frame(root)
embed_frame.pack(fill='both', expand=True)
os.environ['SDL_WINDOWID'] = str(embed_frame.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
pygame.init()
screen = pygame.display.set_mode((200, 100))
pygame.event.set_blocked([pygame.MOUSEMOTION, pygame.ACTIVEEVENT])
while True:
root.update()
for event in pygame.event.get():
print(event)
screen.fill((255, 255, 255))
if pygame.key.get_mods() & pygame.KMOD_SHIFT:
screen.fill((50, 205, 50))
if …Run Code Online (Sandbox Code Playgroud)