-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.py
145 lines (120 loc) · 4.27 KB
/
Utils.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
#!/usr/bin/python3
'''
Utils.py
Copyright (C) 2015 - Bill Williams
Modified by Markus Barth 2022
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
'''
import tkinter
from tkinter import ttk
from tkinter.colorchooser import askcolor
import PIL
from PIL import Image, ImageTk, ExifTags
from DialogInt import *
from Tooltip import *
#
# General utility functions
#
def UnderConstruction ( window ):
Label(window,text='UNDER CONSTRUCTION',font=('Arial',14,('bold')),
anchor='center').grid(row=0,column=0,sticky='EW')
def OnOff ( val ): return 'On' if val else 'Off'
def EvenOdd ( val ): return 'even' if val else 'odd'
# Add BooleanVar in here
def MyRadio ( f, txt, varValue, varName, cmd=None, row=0, col=0, stick='W',
span=1, pad=(5,5,5,5), tip='No Tip number provided'):
#def MyRadio ( f, txt, varValue, varName = None, cmd=None, row=0, col=0, stick='W',
# span=1, pad=(5,5,5,5), tip='No Tip number provided'):
# if varName is None:
# # Determine type of var from varValue and create one
# if type(varValue) is int:
# varName = MyIntVar(varValue)
# elif type(varValue) is boolean:
# varName = MyBooleanVar(varValue)
# elif type(varValue) is str:
# varName = MyStringVar(varValue)
if cmd is None:
r = ttk.Radiobutton(f,text=txt,value=varValue,variable=varName,
padding=pad)
else:
r = ttk.Radiobutton(f,text=txt,value=varValue,variable=varName,
command=lambda:cmd(varValue),padding=pad)
r.grid(row=row,column=col,sticky=stick, columnspan=span)
ToolTip(r,msg=tip)
return r # , varName # return RadioButton and BooleanVar
'''
params = [ ['text', True or False, row, col, sticky, rowspan, tipNum]
['text', True or False, row, col, sticky, rowspan, tipNum] ]
Command = the function to call if pressed, pass True or False
Create two radio buttons, return the first one, and the BooleanVar
associated with the two. If command is not None, then call the command
with the same value passed in the list
Return, first radio created, BooleanVar created
'''
def CreateRadioButtonBoolean ( parent, params, command=None ):
pass
'''
params = [ ['text', 'value', row, col, sticky, rowspan, tipNum]
['text', 'value', row, col, sticky, rowspan, tipNum] ]
Command = the function to call if pressed, pass True or False
Create two radio buttons, return the first one, and the BooleanVar
associated with the two. If command is not None, then call the command
with the same value passed in the list
Return, first radio created, StringVar created
'''
def CreateRadioButtonSet ( parent, params, command=None ):
pass
def MyComboBox ( parent, values, current, callback, width=5, row=0, col=0,
sticky='EW', state='disabled', tip='No Tip number provided' ):
combo = ttk.Combobox(parent,state=state,width=width)
combo.grid(row=row,column=col,sticky=sticky)
combo.bind('<<ComboboxSelected>>',callback)
combo['values'] = values
combo.current(current)
ToolTip(combo,tip)
return combo
def MySliderBar ( parent ):
pass
def MyEditField ( parent ):
pass
def MyLabel ( parent, text, row, col, span ):
pass
def MyButton ( parent ):
pass
def MyLabelFrame ( f, txt, row, col, stick='NEWS', py=5, span=1, pad=(5,5,5,5)):
l = ttk.LabelFrame(f,text=txt,padding=pad)
l.grid(row=row,column=col,sticky=stick,columnspan=span,pady=py)
return l
def MyBooleanVar ( setTo ):
b = BooleanVar()
b.set(setTo)
return b
def MyIntVar ( setTo ):
b = IntVar()
b.set(setTo)
return b
def MyStringVar ( setTo ):
s = StringVar()
s.set(setTo)
return s
def GetPhotoImage ( filename ):
# Get the image - test whether python 2x or 3x
if isinstance(filename,str):
return ImageTk.PhotoImage(PIL.Image.open(filename))
else:
return ImageTk.PhotoImage(filename)
def USECtoSec ( usec ):
# return a text value formatted
if usec < 1000:
return '%d usec' % usec
elif usec < 1000000:
return '%.3f msec' % (float(usec) / 1000.0)
else:
return '%.3f sec' % (float(usec) / 1000000.0)