Forum: PC-Programmierung PyQtGraph, Python3, addLabel // Stringformatierung


von M. L. (ado)


Angehängte Dateien:

Lesenswert?

Ich habe Probleme einen Ausgabestring richtig zu formatieren.
Nach den 3 Graphenplots soll eine oder mehrere Zeilen Text stehen in dem 
mehrere Variablen ausgegeben werden sollen. Kurze Zahlen sollen mit 
Leerzeichen aufgefüllt werden, so daß die Ausgaben nicht hin und 
herspringen. Ich habe den Eindruck, daß dies unter pyqtgraph direkt 
nicht möglich ist, und ich in irgendwelchen Qt Widgets da mit einbringen 
muß.
Ich bin Pythonnoob. Wenn mir jemand die richtige Richtung weisen könnte 
wäre das sehr nett.

Ich habe ein Label erzeugt

win.nextRow()
dataLabel = win.addLabel(dataString)

welches ich auch mit Text füllen kann.

dataLabel.setText(str(counter) + " " + " || "
                            + str(data11[-1]) + " " + str(data12[-1]) + 
" " + str(data13[-1]) + " || "
                            + str(data21[-1]) + " " + str(data22[-1]) + 
" " + str(data23[-1]) + " || "
                            + str(data31[-1]) + " " + str(data32[-1]) + 
" " + str(data33[-1]) + " || ")



Nur für die richtige Stringformatierung scheine ich zu doof zu sein.


1
# -*- coding: utf-8 -*-
2
"""
3
__
4
"""
5
6
7
import pyqtgraph as pg
8
from pyqtgraph.Qt import QtCore, QtGui
9
import numpy as np
10
# import pyqtgraph.opengl as gl
11
12
13
win = pg.GraphicsWindow()
14
#  win = pg.mkQApp()
15
16
win.showMaximized()  # win.resize(1400,800)
17
18
win.setWindowTitle('DataViewer:       9-DOF')
19
20
21
# 3) Plot in chunks, adding one new plot curve for every 100 samples
22
23
accX = 12345
24
accY = 67890
25
accZ = 23456
26
27
gX = 12340
28
gY = 54320
29
gZ = 55210
30
31
magX = 23450
32
magY = 25250
33
magZ = 11120
34
35
counter = 12345
36
chunkSize = 100
37
dataBufferSize = 1600
38
dataString = str(counter) + " Moin 0004 09876  34564 32453  23243  32144"
39
# Remove chunks after we have 10
40
maxChunks = 10
41
tmax = 150
42
signemax = 200
43
44
startTime = pg.ptime.time()
45
46
plot1 = win.addPlot(title="Acceleration")
47
48
plot1.setLabel('left', 'ACC', 'g')
49
plot1.showGrid(x=True, y=True, alpha=0.3)
50
plot1.setXRange(0, -300)
51
# plot1.setRange(xRange=[100, 0])
52
# plot1.setLimits(xMax=0)
53
# plot1.setYRange(-10, 10)
54
plot1.disableAutoRange(False)
55
plot1.invertX(True)
56
plot1.setMouseEnabled(x=False, y=True)
57
58
win.nextRow()
59
60
plot2 = win.addPlot(title="Gyro")
61
62
# plot2.setLabel('bottom', 'Time', 's')
63
plot2.setLabel('left', 'Gyro', /s')
64
plot2.showGrid(x=True, y=True, alpha=0.3)
65
# plot2.setXRange(-10, 0)
66
# plot2.setRange(xRange=[-100, 0])
67
# plot2.setYRange(-10, 10)
68
plot2.disableAutoRange(False)
69
plot2.invertX(True)
70
plot2.setXRange(0, -300)
71
plot2.setMouseEnabled(x=False, y=True)
72
73
win.nextRow()
74
75
plot3 = win.addPlot(title="Magnetometer")
76
# plot3 = win.addPlot(title="Magnetometer",colspan=2)
77
plot3.setLabel('bottom', 'Time', 's')
78
plot3.setLabel('left', 'MAG', 'T')
79
plot3.showGrid(x=True, y=True, alpha=0.3)
80
# plot3.setXRange(-10, 0)
81
# plot3.setYRange(-10, 10)
82
plot3.disableAutoRange(False)
83
plot3.invertX(True)
84
plot3.setXRange(0, -300)
85
plot3.setMouseEnabled(x=False, y=True)
86
87
win.nextRow()
88
89
dataLabel = win.addLabel(dataString)
90
# dataLabel.setLabel("left")
91
92
dataLabel.setText(str(counter))  # + "0004 16328 32768 12345 09876 34564 43233"
93
94
95
# #######################################################################################################
96
97
# 2. OpenGl 3d Fenster
98
# w = gl.GLViewWidget()
99
# w.show()
100
# w.setWindowTitle('pyqtgraph example: GLMeshItem')
101
# w.setCameraPosition(distance=40)
102
103
# g = gl.GLGridItem()
104
# g.scale(2,2,1)
105
# w.addItem(g)
106
107
# ######################################################################################################################
108
109
110
data11 = np.random.normal(size=dataBufferSize)
111
data12 = np.random.normal(size=dataBufferSize)
112
data13 = np.random.normal(size=dataBufferSize)
113
data21 = np.random.normal(size=dataBufferSize)
114
data22 = np.random.normal(size=dataBufferSize)
115
data23 = np.random.normal(size=dataBufferSize)
116
data31 = np.random.normal(size=dataBufferSize)
117
data32 = np.random.normal(size=dataBufferSize)
118
data33 = np.random.normal(size=dataBufferSize)
119
120
curve11 = plot1.plot(data11, pen='r')
121
curve12 = plot1.plot(data12, pen='g')
122
curve13 = plot1.plot(data13, pen='b')
123
124
curve21 = plot2.plot(data21, pen='r')
125
curve22 = plot2.plot(data22, pen='g')
126
curve23 = plot2.plot(data23, pen='b')
127
128
curve31 = plot3.plot(data31, pen='r')
129
curve32 = plot3.plot(data32, pen='g')
130
curve33 = plot3.plot(data33, pen='b')
131
132
133
ptr1 = -dataBufferSize
134
135
136
# ############################################################################
137
138
def update1():
139
    global data11, data12, data13
140
    global data21, data22, data23
141
    global data31, data32, data33
142
    global curve11, curve12, curve13
143
    global curve21, curve22, curve23
144
    global curve31, curve32, curve33
145
    global ptr1, counter, dataString, dataLabel
146
147
# shift data in the array one sample left # (see also: np.roll)
148
    data11[:-1] = data11[1:]
149
    data11[-1] = np.random.randint(0, 6786)
150
    data12[:-1] = data12[1:]
151
    data12[-1] = np.random.randint(0, 6786)
152
    data13[:-1] = data13[1:]
153
    data13[-1] = np.random.randint(0, 6786)
154
155
    data21[:-1] = data21[1:]
156
    data21[-1] = np.random.randint(0, 6786)
157
    data22[:-1] = data22[1:]
158
    data22[-1] = np.random.randint(0, 6786)
159
    data23[:-1] = data23[1:]
160
    data23[-1] = np.random.randint(0, 6786)
161
162
    data31[:-1] = data31[1:]
163
    data31[-1] = np.random.randint(0, 6786)
164
    data32[:-1] = data32[1:]
165
    data32[-1] = np.random.randint(0, 6786)
166
    data33[:-1] = data33[1:]
167
    data33[-1] = np.random.randint(0, 6786)
168
169
    curve11.setData(data11)
170
    curve12.setData(data12)
171
    curve13.setData(data13)
172
173
    curve21.setData(data21)
174
    curve22.setData(data22)
175
    curve23.setData(data23)
176
177
    curve31.setData(data31)
178
    curve32.setData(data32)
179
    curve33.setData(data33)
180
181
    curve11.setPos(ptr1, 0)
182
    curve12.setPos(ptr1, 0)
183
    curve13.setPos(ptr1, 0)
184
    curve21.setPos(ptr1, 0)
185
    curve22.setPos(ptr1, 0)
186
    curve23.setPos(ptr1, 0)
187
    curve31.setPos(ptr1, 0)
188
    curve32.setPos(ptr1, 0)
189
    curve33.setPos(ptr1, 0)
190
191
    ptr1 += 1
192
    ptr1 -= 1
193
    counter += 1
194
195
     #   dataLabel.setText(str(counter) + " " + " || " +
196
 #    str(accX) + " " + str(accY) + " " + str(accZ) + " || " +
197
 #    str(gX) + " " + str(gY) + " " + str(gZ) + " || " +
198
 #    str(magX) + " " + str(magY) + " " + str(magZ) + " || ")
199
200
    dataLabel.setText(str(counter) + " " + " || "
201
                            + str(data11[-1]) + " " + str(data12[-1]) + " " + str(data13[-1]) + " || "
202
                            + str(data21[-1]) + " " + str(data22[-1]) + " " + str(data23[-1]) + " || "
203
                            + str(data31[-1]) + " " + str(data32[-1]) + " " + str(data33[-1]) + " || ")
204
205
#     str(counter*2)+''+str(counter*3))#+'mice'.ljust(8, '.').center(40))
206
207
#   dataLabel.setText(str(counter)+
208
#   str(accX)+" "+str(accY)+" "+str(accZ)+"___"+
209
#   str(gX)+" "+str(gY)+" "+str(gZ)+"___"+
210
#   str(magX)+" "+str(magY)+" "+str(magZ)+" "+
211
#   str(counter*2)+''+str(counter*3))#+'mice'.ljust(8, '.').center(40))
212
213
#   dataLabel.setText(dataString)
214
215
#  ######################################################################################################################
216
217
# update all plots
218
219
def update():
220
    update1()
221
222
223
timer = pg.QtCore.QTimer()
224
timer.timeout.connect(update)
225
timer.start(10)
226
227
# Start Qt event loop unless running in interactive mode or using pyside.
228
if __name__ == '__main__':
229
    import sys
230
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
231
        QtGui.QApplication.instance().exec_()

: Bearbeitet durch User
von Kaj (Gast)


Lesenswert?


von M. L. (ado)


Lesenswert?

Soweit funktioniert das erst einmal.

1
dataLabel.setText('{0:05d} || {0:05d} {0:05d} {0:05d} || {0:05d} {0:05d} {0:05d} || {0:05d} {0:05d} {0:05d} ||'
2
.format(int(counter), int(data11[-1]), int(data12[-1]), int(data13[-1]), int(data21[-1]), int(data22[-1])
3
, int(data23[-1]), int(data31[-1]), int(data32[-1]), int(data33[-1])))


Vielen Dank Kaj. Deine Antwort hat mir sehr geholfen.

Jetzt kann ich mich auf die Gegenseite stürzen und anfangen die Firmware 
zu schreiben.

: Bearbeitet durch User
von Kaj (Gast)


Lesenswert?

M. L. schrieb:
> Vielen Dank Kaj. Deine Antwort hat mir sehr geholfen.
Kein Ding, dafuer ist das Forum ja da. :)

von M. L. (ado)


Lesenswert?

Und hiermit funktioniert es auch wirklich. :-)

1
    dataLabel.setText('{0:05d} || {1:05d} {2:05d} {3:05d} || {4:05} {5:05d} {6:05d} || {7:05} {8:05d} {9:05d} ||'
2
        .format((counter), int(data11[-1]), int(data12[-1]), int(data13[-1]), int(data21[-1]), int(data22[-1]), int(data23[-1])
3
            , int(data31[-1]), int(data32[-1]), int(data33[-1]) ) )

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.