Repository navigation
Expand file tree
/
Copy pathsimula_TextItem.py
More file actions
100 lines (75 loc) · 2.86 KB
/
Copy pathsimula_TextItem.py
File metadata and controls
100 lines (75 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore
x = np.linspace(-20, 20, 1000)
y = np.sin(x) / x
plot = pg.plot() ## create an empty plot widget
plot.setYRange(-1, 2)
plot.setWindowTitle('pyqtgraph example: text')
plot.setZValue(0)
curve = plot.plot(x, y) ## add a single curve
## Create text object, use HTML tags to specify color/size
text = pg.TextItem(
html='<div style="text-align: center"><span style="color: #FFF;">This is the</span><br><span style="color: #FF0; font-size: 16pt;">PEAK</span></div>',
anchor=(-0.3, 0.5), angle=45, border='w', fill=(0, 0, 255, 100))
text.setZValue(1000)
plot.addItem(text)
text.setPos(0, y.max())
## Draw an arrowhead next to the text box
arrow = pg.ArrowItem(pos=(0, y.max()), angle=-45)
plot.addItem(arrow)
## Set up an animated arrow and text that track the curve
curvePoint = pg.CurvePoint(curve)
plot.addItem(curvePoint)
text2 = pg.TextItem("test", anchor=(0.5, -1.0))
text2.setParentItem(curvePoint)
arrow2 = pg.ArrowItem(angle=45)
# arrow2.setParentItem(curvePoint)
linecur = pg.InfiniteLine(angle=90, movable=True)
plot.addItem(linecur)
## update position every 10ms
index = 0
def update():
global curvePoint, index
index = (index + 1) % len(x)
nuova_posizione_x = x[index]
curvePoint.setPos(float(index) / (len(x) - 1))
linecur.setPos(nuova_posizione_x)
text2.setText('[%0.1f, %0.1f]' % (x[index], y[index]))
timer = QtCore.QTimer()
timer.timeout.connect(update)
# timer.start(10)
update()
###############################
### ------------------------------------ ###
### NUOVO: GESTIONE PRESSIONE TASTI ###
### ------------------------------------ ###
# 1. Definiamo la funzione che gestirà l'evento
def gestisci_tasti(event):
# 'event' contiene le informazioni su quale tasto è stato premuto
# Definiamo di quanto si sposta la linea a ogni pressione
# Sentiti libero di cambiare questo valore!
step = 0.1
# Prendiamo la posizione X attuale della linea
posizione_attuale = linecur.value()
# 2. Controlliamo quale tasto è stato premuto
if event.key() == QtCore.Qt.Key.Key_Right:
# Se è la freccia destra, sposta la linea a destra
# linecur.setPos(posizione_attuale + step)
update()
elif event.key() == QtCore.Qt.Key.Key_Left:
# Se è la freccia sinistra, sposta la linea a sinistra
# linecur.setPos(posizione_attuale - step)
update()
# Nota: se premiamo altri tasti, questa funzione
# semplicemente non fa nulla.
# 3. Colleghiamo la nostra funzione al widget del grafico
plot.keyPressEvent = gestisci_tasti
# 4. FONDAMENTALE: Diamo il "focus" al grafico.
# Solo il widget che ha il "focus" (cioè quello attivo)
# può "sentire" la pressione dei tasti.
# Potrebbe essere necessario cliccare prima sul grafico
# per fargli ottenere il focus.
plot.setFocus()
if __name__ == '__main__':
pg.exec()