forked from crxguy52/serialplot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfgWindow.py
638 lines (538 loc) · 27.2 KB
/
cfgWindow.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
'''
ConfigFrame is a class that gets packed into the root window. Takes config
settings from the user and passes them into the approrpate functions when the
Go button is pressed
'''
import sys
if sys.version_info[0] < 3:
#If we're executing with Python 2
import Tkinter as tk
import tkMessageBox as messagebox
import ttk
import tkFileDialog as filedialog
import tkColorChooser as colorchooser
else:
#Otherwise we're using Python 3
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
from tkinter import filedialog
from tkinter import colorchooser
from graphWindow import *
import serial.tools.list_ports
import pprint
from defaults import defaults
import os
class ConfigFrame(ttk.Frame):
"""
Main application Frame. Houses all the individual objects (classes) that
make up the application
"""
def __init__(self, parent):
ttk.Frame.__init__(self, parent)
self.parent = parent
self['padding'] = '4'
self.TKvariables = {}
#Read in the defaults
for key in defaults:
if key[0:5] == 'graph' or key.find('ylims') >= 0:
self.TKvariables.update({key:[]})
for val in range(len(defaults[key])):
self.TKvariables[key].append(tk.StringVar(value=defaults[key][val]))
else:
self.TKvariables.update({key:tk.StringVar(value=defaults[key])})
num_vars = int(self.TKvariables['datalength'].get())
self.datalist = list(range(1,num_vars+1))
#Create a combobox containing the available COM ports
comlst = self.get_comlst()
self.COMbox = ttk.Labelframe(self, text='COM port to source data from')
self.COMcombo = ttk.Combobox(self.COMbox, width=60, values=comlst, \
state='readonly', textvariable=self.TKvariables['COMport'],\
postcommand=self.updateCOMbox )
self.COMbox.grid(row = 0, column = 0, columnspan = 5)
self.COMcombo.grid()
#Create an "about" text box
ABOUTframe = ttk.LabelFrame(self, text = 'What it does')
ABOUTlabel = ttk.Label(ABOUTframe, text= \
'Graphs data coming in over the serial port in a comma '
'seperated variable string. Hover over each option to get '
'a description of what the setting does', wraplength = 140)
ABOUTframe.grid(row=1, column = 0, rowspan = 2, columnspan = 2, \
sticky = 'nw, se', padx= 3, pady = 5)
CreateToolTip(ABOUTlabel,\
"The default values can be changed by opening defaults.py with a text "
"editor and changing the values")
ABOUTlabel.pack()
#Create a Graph! and About buttons
GObut = ttk.Button(self, text='Go!', command=self.goButton)
GObut.grid(row=6, column = 0, sticky = 'we')
ABOUTbut = ttk.Button(self, text='About', command=self.aboutButton)
ABOUTbut.grid(row = 6, column = 1, sticky = 'we')
#Create an instance of the class for the config panel
notebook = ConfigNotebook(self, self)
#Update the state of the graphs based on the defaults and grid
notebook.updateGraphs()
notebook.grid(row=1, column=3, columnspan=2, rowspan=6, sticky = 'nsew', \
padx = 5, pady = 5)
#Bind the enter key to start the program
self.parent.bind("<Return>", lambda event:self.goButton())
def getfilename(self):
if self.TKvariables['log2file'].get() == 'on':
#Only pop up with the dialog when the box is checked
options = {}
options['filetypes'] = [('Comma Seperated Variable', '.csv')]
options['initialfile'] = 'GraphLog.csv'
self.TKvariables['filename'].set(filedialog.asksaveasfilename(**options))
def goButton(self):
self.parent.variables = {}
for key in self.TKvariables:
if key[0:5] == 'graph' or key.find('ylims') >= 0:
self.parent.variables.update({key:[]})
for val in range(len(self.TKvariables[key])):
self.parent.variables[key].append(self.TKvariables[key][val].get())
else:
self.parent.variables.update({key:self.TKvariables[key].get()})
if self.parent.variables['COMport'] == '':
messagebox.showerror(message='Select a COM port!')
else:
try:
#Try to open the COM port first to make sure it's available
if os.name == 'nt':
s = serial.Serial(port=self.parent.variables['COMport'][0:4])
else:
first_space = self.parent.variables['COMport'].index(' ')
# Parameters necessary due to https://github.com/pyserial/pyserial/issues/59
s = serial.Serial(port=self.parent.variables['COMport'][0:first_space], rtscts=True, dsrdtr=True)
s.close()
GraphTopLevel(self.parent)
except Exception as e:
#Otherwise the port isn't available, so error out
messagebox.showerror(message=('COM port not available: ', e))
def aboutButton(self):
toplvl = tk.Toplevel()
toplvl.title('About')
txt = ttk.Label(toplvl, wraplength=450, text= \
"This program was written by Victor Zaccardo as a way to familiarize "
"myself with Python, and also so I don't have to try and read a serial"
" terminal every time I want to visualize data coming out of a "
"microcontroller. It's written in Python 2.7, using tkinter, "
"matplotlib, and pyserial. \n \n I hope it can be helpful with your "
"embedded projects. If you have any questions or comments, feel free "
"to contact me at victorzaccardo@gmail.com. Happy plotting!")
txt.grid(row=0, column=0, padx=5, pady=5)
closeButton = ttk.Button(toplvl, text='Close', command=toplvl.destroy)
closeButton.grid(row=1, column=0, pady = 3)
toplvl.update()
scrwidth = toplvl.winfo_screenwidth()
scrheight = toplvl.winfo_screenheight()
winwidth = toplvl.winfo_reqwidth()
winheight = toplvl.winfo_reqheight()
winposx = int(round(scrwidth/2 - winwidth/2))
winposy = int(round(scrheight/2 - winheight/2))
toplvl.geometry('{}x{}+{}+{}'.format(winwidth, winheight, winposx, winposy))
def get_comlst(self):
"""Returns a list of available COM ports with description"""
comports = serial.tools.list_ports.comports()
comlst = []
for item in comports:
name = item[0]
if len(item[1]) > 50:
description = item[1][0:44] + "..."
else:
description = item[1]
comlst.append(str(name + " - " + description))
return sorted(comlst)
def updateCOMbox(self):
self.COMcombo['values'] = self.get_comlst()
class ConfigNotebook(ttk.Notebook):
"""
A notebook that houses all the configuration for the program. Calls classes
that configure each tab individually and places them in the notebook.
Note on the controller - it's the top level of the application (an instance
of MainApplication). It will have a dictionary that houses all the
TKvariables, makes accessing them easier.
"""
def __init__(self, parent, controller):
ttk.Notebook.__init__(self, parent)
self.controller = controller
datalist = list(range(1,7))
datalist.insert(0,'-')
#Create the pages
serialcfgframe = SerialTab(self, self.controller)
datacfgframe = DataTab(self, self.controller)
graph1frame = GraphTab(self, self.controller, 1)
graph2frame = GraphTab(self, self.controller, 2)
graph3frame = GraphTab(self, self.controller, 3)
#Add them to the notebook
self.add(datacfgframe, text='Data')
self.add(serialcfgframe, text='Serial')
self.add(graph1frame, text='Graph 1')
self.add(graph2frame, text='Graph 2')
self.add(graph3frame, text='Graph 3')
def updateGraphs(self, *args, **kwargs):
num_graphs = int(self.controller.TKvariables['numgraphs'].get())
#First, disable all the graphs
for i in range(2, 5):
self.tab(i, state='disabled')
#Now, re-enable based on how many graphs are selected
if num_graphs >= 1:
self.tab(2, state='normal')
if num_graphs >= 2:
self.tab(3, state='normal')
if num_graphs >= 3:
self.tab(4, state='normal')
class SerialTab(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
self.controller = controller
self['padding'] = [0, 7, 0, 0]
#Populate the serial configuration tab
self.baudlist = (4800, 9600, 19200, 38400, 57600, 115200, 230400, 921600)
self.databitslist = (7, 8)
self.stopbitslist = (1, 2)
self.paritylist = ('None', 'Even', 'Odd', 'Mark', 'Space')
baudlabel = ttk.Label(self, text='Baudrate')
baudbox = ttk.Combobox(self, width=8, values=self.baudlist,
textvariable=self.controller.TKvariables['baud'])
datalabel = ttk.Label(self, text='Data bits')
databox = ttk.Combobox(self, width=8, values = self.databitslist, \
textvariable=self.controller.TKvariables['databits'])
stopbitslabel = ttk.Label(self, text='Stop bits')
stopbitsbox = ttk.Combobox(self, width=8, values=self.stopbitslist, \
textvariable=self.controller.TKvariables['stopbits'])
paritylabel = ttk.Label(self, text='Parity')
paritybox = ttk.Combobox(self, width=8, values=self.paritylist, \
textvariable=self.controller.TKvariables['parity'])
#ttk.Label(self, text=' ').grid(row=1, column=0)
baudlabel.grid(row=1, column = 1, padx=5)
baudbox.grid(row=1, column=2, padx=5)
datalabel.grid(row=2, column = 1, padx=5)
databox.grid(row=2, column=2, padx=5)
stopbitslabel.grid(row=3, column = 1, padx=5)
stopbitsbox.grid(row=3, column=2, padx=5)
paritylabel.grid(row=4, column = 1, padx=5)
paritybox.grid(row=4, column=2, padx=5)
class DataTab(ttk.Frame):
"""
Houses configuration for the incoming data
"""
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
self['padding'] = 4
self.parent = parent
self.controller = controller
self.datalist = list(range(1,11))
self.terminatorlist = ['\\n', ';', '\\n;']
self.numgraphslist = list(range(1,4))
#How long is the data coming in?
datalabel = ttk.Label(self, text='Variables per line')
databox = ttk.Combobox(self, width=8, values = self.datalist, \
textvariable=self.controller.TKvariables['datalength'])
CreateToolTip(datalabel,\
"The numbder of variables per line. "
"A line is a series of variables seperated by a comma, and terminated by a \\n character. "
"For example, the line: data1, data2, data3\\n would have 3 variables. "
"All data recieved must be a string, no binary numbers allowed")
maxlabel = ttk.Label(self, text='Max Message Length')
maxbox = ttk.Entry(self, width=11, \
textvariable=self.controller.TKvariables['maxlength'])
CreateToolTip(maxlabel, \
'The maximum length of one line (in characters). If anything '
'be conservative with this number, err on the high side. The program reads '
'lines from the serial buffer until it is below this number of characters, to avoid '
'a condition where it tries to read a line out of the serial buffer and a \\n '
"can't be found"
)
numgraphslabel = ttk.Label(self, text='Number of graphs')
numgraphsbox = ttk.Combobox(self, width=8, values=self.numgraphslist, \
textvariable=self.controller.TKvariables['numgraphs'])
numgraphsbox.bind('<<ComboboxSelected>>', self.parent.updateGraphs)
CreateToolTip(numgraphslabel,\
"The number of graphs to plot data on")
maxcheck = ttk.Checkbutton(self, text='Start Maximized?', \
variable=self.controller.TKvariables['startmax'], \
onvalue='yes', offvalue='no')
CreateToolTip(maxcheck, \
"When the graph is started, the window will be maximized.")
log2filecheck = ttk.Checkbutton(self, text='Log to file?',\
variable=self.controller.TKvariables['log2file'], onvalue='on', \
offvalue='off', command=self.controller.getfilename)
CreateToolTip(log2filecheck, \
"If checked, all data recieved will also be logged to a CSV file")
AObutton = ttk.Button(self, text='Advanced Options', command=self.AObutton)
datalabel.grid(row=1, column = 1, sticky='w')
databox.grid(row=1, column = 2, sticky='w', padx=7)
maxlabel.grid(row=2, column=1, sticky='w')
maxbox.grid(row=2, column=2, sticky='w', padx=7)
numgraphslabel.grid(row=3, column=1, sticky='w')
numgraphsbox.grid(row=3, column=2, sticky='w', padx=7)
maxcheck.grid(row=4, column=1, columnspan=2, sticky='w')
log2filecheck.grid(row=5, column=1, columnspan=2, sticky='w')
AObutton.grid(row=6, column=1, columnspan=2, sticky='ew')
def AObutton(self):
toplvl = tk.Toplevel()
toplvl.withdraw()
frame = ttk.Frame(toplvl, padding=[4, 4, 4, 4])
boxwidth = 8
boxpadx = 5
TKvars = self.controller.TKvariables
#Data Depth
datalabel = ttk.Label(frame, text='Data History Depth')
databox = ttk.Entry(frame, width=boxwidth, textvariable=TKvars['datadepth'])
datapostlbl = ttk.Label(frame, text='Lines')
datalabel.grid(row=0, column=0, sticky='e')
databox.grid(row=0, column=1, sticky='ew', padx=boxpadx)
datapostlbl.grid(row=0, column=2, sticky='w')
CreateToolTip(datalabel, \
'How many lines of data to plot on the x axis. More = longer history '
'displayed on the screen')
#Refresh Frequency
refreshlabel = ttk.Label(frame, text='Refresh Frequency')
refreshbox = ttk.Entry(frame, width=boxwidth, textvariable=TKvars['refreshfreq'])
refreshpostlbl = ttk.Label(frame, text='Hz')
refreshlabel.grid(row=1, column=0, sticky='e')
refreshbox.grid(row=1, column=1, sticky='ew', padx=boxpadx)
refreshpostlbl.grid(row=1, column=2, sticky='w')
CreateToolTip(refreshlabel, \
'How often to redraw the screen. Any value higher than what your PC '
'can do will just max out the process. A reasonable value to start '
'with is 20')
#Data Width
widthlabel = ttk.Label(frame, text='Statusbar Data Width')
widthbox = ttk.Entry(frame, width=boxwidth, textvariable=TKvars['stsbrwdth'])
widthpostlbl = ttk.Label(frame, text='Chars')
widthlabel.grid(row=2, column=0, sticky='e')
widthbox.grid(row=2, column=1, sticky='ew', padx=boxpadx)
widthpostlbl.grid(row=2, column=2, sticky='w')
CreateToolTip(widthlabel, \
'This is for keeping the "last line recieved" value in the statusbar'
'a constant width. If you find that the statusbar is jumping around, '
'increase this value')
#Set as defaults
defaultbutton = ttk.Button(frame, text='Set selections as defaults', \
command=self.setDefaults)
defaultbutton.grid(row=3, column=0, columnspan=1, pady=1, sticky='ww')
CreateToolTip(defaultbutton, \
'Set ALL the current settings as the defaults')
#OK button
OKbutton = ttk.Button(frame, text='OK', width=10, command=toplvl.destroy)
OKbutton.grid(row=3, column=1, columnspan=2, pady=1, sticky='e')
frame.grid()
toplvl.update()
scrwidth = toplvl.winfo_screenwidth()
scrheight = toplvl.winfo_screenheight()
winwidth = toplvl.winfo_reqwidth()
winheight = toplvl.winfo_reqheight()
winposx = int(round(scrwidth/2 - winwidth/2))
winposy = int(round(scrheight/2 - winheight/2))
toplvl.geometry('{}x{}+{}+{}'.format(winwidth, winheight, winposx, winposy))
toplvl.deiconify()
def setDefaults(self):
defaultstmp = {}
TKvars = self.controller.TKvariables
for key in TKvars:
if key[0:5] == 'graph' or key.find('ylims') >= 0:
defaultstmp.update({key:[]})
for val in range(len(TKvars[key])):
try:
defaultstmp[key].append(int(TKvars[key][val].get()))
except:
defaultstmp[key].append(TKvars[key][val].get())
elif key == 'filename':
#There is a bug with pprint that puts a u in front of the
#filename, so convert it to a string first
defaultstmp.update({key:str(TKvars[key].get())})
else:
try:
defaultstmp.update({key:int(TKvars[key].get())})
except:
defaultstmp.update({key:TKvars[key].get()})
fileobj = open('defaults.py', 'w')
header = \
"'''\n" \
"Be careful when modifying these values - if they aren't set correctly, \n" \
"the program won't run. As a precaution if you modify it, it's a good idea to \n" \
"save a copy first. serialplot just looks for a file in the same directory \n" \
"called 'defaults.py'\n \n" \
"The format for graphXlineX properties is:\n"\
"[datalabel,\ndatapos,\nlinecolor,\ndashed,\nmultiplier,\noffset]\n"\
"'''\n\n"
fileobj.write(header)
fileobj.write('defaults = ' + pprint.pformat(defaultstmp) + '\n')
fileobj.close()
class GraphTab(ttk.Frame):
def __init__(self, parent, controller, graphnum):
ttk.Frame.__init__(self, parent)
self.controller = controller
self['padding'] = [4, 4, 0, 0]
key1 = 'graph' + str(graphnum) + 'line1'
key2 = 'graph' + str(graphnum) + 'line2'
key3 = 'graph' + str(graphnum) + 'line3'
data1 = self.controller.TKvariables[key1][1]
color1 = self.controller.TKvariables[key1][2]
data2 = self.controller.TKvariables[key2][1]
color2 = self.controller.TKvariables[key2][2]
data3 = self.controller.TKvariables[key3][1]
color3 = self.controller.TKvariables[key3][2]
#Create 3 comboboxes to select up to 3 datas to plot
data1label = ttk.Label(self, text='Data 1 position in string')
self.data1box = ttk.Combobox(self, width=3, values=self.controller.datalist, \
textvariable=data1, postcommand=self.updatecblist)
data1color = tk.Button(self, bg=color1.get(), width=1,\
command=lambda:self.setcolor(data1color,1,1,color1))
CreateToolTip(data1label,\
"The position of the first value to plot in the incoming line. It is one indexed, so "
"the first value is in position 1")
data2label = ttk.Label(self, text='Data 2 position in string')
self.data2box = ttk.Combobox(self, width=3, values=self.controller.datalist, \
textvariable=data2, postcommand=self.updatecblist)
data2color = tk.Button(self, bg=color2.get(), width=1,\
command=lambda:self.setcolor(data2color,1,2,color2))
CreateToolTip(data2label,\
"The position of the second value in the incoming line. It is one indexed, so "
"the first value is in position 1")
data3label = ttk.Label(self, text='Data 3 position in string')
self.data3box = ttk.Combobox(self, width=3, values=self.controller.datalist, \
textvariable=data3, postcommand=self.updatecblist)
data3color = tk.Button(self, bg=color3.get(), width=1,\
command=lambda:self.setcolor(data3color,1,3,color3))
CreateToolTip(data3label,\
"The position of the third value in the incoming line. It is one indexed, so "
"the first value is in position 1")
#Create an advanced options button
AObutton = ttk.Button(self, text='Advanced Options', \
command=lambda:self.AObutton(graphnum))
data1label.grid(row=1, column = 1, columnspan=3, sticky='w', pady = 3)
self.data1box.grid(row=1, column=4, sticky='w', padx = 5)
data1color.grid(row=1, column=5, padx=2)
data2label.grid(row=2, column = 1, columnspan=3, sticky='w', pady = 3)
self.data2box.grid(row=2, column=4, sticky='w', padx = 5)
data2color.grid(row=2, column=5)
data3label.grid(row=3, column=1, columnspan=3, sticky='w', pady = 3)
self.data3box.grid(row=3, column=4, sticky='w', padx = 5)
data3color.grid(row=3, column=5)
#Ymin\Ymax
key = 'g'+str(graphnum)+'ylims'
ttk.Label(self, text='Ymin').grid(row=4, column=1, sticky='w')
ttk.Entry(self, width=5, textvariable=self.controller.TKvariables[key][0] \
).grid(row=4, column=2, sticky='ew')
ttk.Label(self, text='Ymax').grid(row=4, column=3, sticky='e', padx=3)
ttk.Entry(self, width=6, textvariable=self.controller.TKvariables[key][1] \
).grid(row=4, column=4, sticky='ew', padx=5)
AObutton.grid(row=5, column=1, columnspan=5, sticky='nsew', pady=6)
def updatecblist(self):
num_vars = int(self.controller.TKvariables['datalength'].get())
self.controller.datalist = list(range(1,num_vars+1))
self.controller.datalist.insert(0, '-')
self.data1box['values'] = self.controller.datalist
self.data2box['values'] = self.controller.datalist
self.data3box['values'] = self.controller.datalist
def setcolor(self, button, graph, line, initialcolor):
color = colorchooser.askcolor(initialcolor=initialcolor.get())
#If the user hits cancel, the dialog returns a "Nonetype" object
#which causes issues, so check for it:
if isinstance(color[1], str):
button['bg'] = color[1]
key = 'graph'+str(graph)+'line'+str(line)
self.controller.TKvariables[key][2].set(value=color[1])
def AObutton(self, graphnum):
toplvl = tk.Toplevel()
toplvl.withdraw()
frame = ttk.Frame(toplvl, padding=[2, 3, 3, 0])
boxwidth = 15
#Create the labels
lbl = ttk.Label(frame, text='Label')
CreateToolTip(lbl, \
'This text will show up in the legend and the log file')
lbl.grid(row=0, column=1)
mult = ttk.Label(frame, text='Multiplier')
CreateToolTip(mult, \
'Multiply by this value')
mult.grid(row=0, column=2)
offset = ttk.Label(frame, text='Offset')
CreateToolTip(offset, \
'Add this value. Happens AFTER the data is multiplied')
offset.grid(row=0, column=3)
dashed = ttk.Label(frame, text='Dashed')
CreateToolTip(dashed, \
'If checked, the line will be dashed')
dashed.grid(row=0, column=4)
ttk.Label(frame, text='Line 1').grid(row=1, column=0, padx=2)
ttk.Label(frame, text='Line 2').grid(row=2, column=0, padx=2)
ttk.Label(frame, text='Line 3').grid(row=3, column=0, padx=2)
for row in range(1,3+1):
key = 'graph'+str(graphnum)+'line'+str(row)
#Label
ttk.Entry(frame, width=boxwidth, \
textvariable=self.controller.TKvariables[key][0]).grid(row=row, column=1)
#Multiplier
ttk.Entry(frame, width=boxwidth, \
textvariable=self.controller.TKvariables[key][4]).grid(row=row, column=2)
#Offset
ttk.Entry(frame, width=boxwidth, \
textvariable=self.controller.TKvariables[key][5]).grid(row=row, column=3)
#Dashed
ttk.Checkbutton(frame, onvalue='--', offvalue='-', \
variable=self.controller.TKvariables[key][3]).grid(row=row, column=4)
ttk.Button(frame, text='OK', command=toplvl.destroy).grid(row=5,\
column=3, columnspan=2, sticky='ew', pady=4)
#Center the window
frame.grid()
toplvl.update()
scrwidth = toplvl.winfo_screenwidth()
scrheight = toplvl.winfo_screenheight()
winwidth = toplvl.winfo_reqwidth()
winheight = toplvl.winfo_reqheight()
winposx = int(round(scrwidth/2 - winwidth/2))
winposy = int(round(scrheight/2 - winheight/2))
toplvl.geometry('{}x{}+{}+{}'.format(winwidth, winheight, winposx, winposy))
toplvl.deiconify()
class CreateToolTip(object):
"""
create a tooltip for a given widget
"""
def __init__(self, widget, text='widget info'):
self.waittime = 500 #miliseconds
self.wraplength = 180 #pixels
self.widget = widget
self.text = text
self.widget.bind("<Enter>", self.enter)
self.widget.bind("<Leave>", self.leave)
self.widget.bind("<ButtonPress>", self.leave)
self.id = None
self.tw = None
def enter(self, event=None):
self.schedule()
def leave(self, event=None):
self.unschedule()
self.hidetip()
def schedule(self):
self.unschedule()
self.id = self.widget.after(self.waittime, self.showtip)
def unschedule(self):
id = self.id
self.id = None
if id:
self.widget.after_cancel(id)
def showtip(self, event=None):
x = y = 0
x, y, cx, cy = self.widget.bbox("insert")
x += self.widget.winfo_rootx() + 25
y += self.widget.winfo_rooty() + 20
# creates a toplevel window
self.tw = tk.Toplevel(self.widget)
# Leaves only the label and removes the app window
self.tw.wm_overrideredirect(True)
self.tw.wm_geometry("+%d+%d" % (x, y))
label = ttk.Label(self.tw, text=self.text, justify='left',
background="#ffffff", relief='solid', borderwidth=1,
wraplength = self.wraplength)
label.pack(ipadx=1)
def hidetip(self):
tw = self.tw
self.tw = None
if tw:
tw.destroy()
#If this script is executed, just run the main script
if __name__ == '__main__':
os.system("serialplot.py")