forked from LeoJr2015/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeter.py
More file actions
68 lines (56 loc) · 2.26 KB
/
meter.py
File metadata and controls
68 lines (56 loc) · 2.26 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
#!/usr/bin/env python
###Meter.py###
from Tkinter import *
import Tkinter
import time
class Meter(Frame):
'''A simple progress bar widget.'''
def __init__(self, master, fillcolor='green', text='',value=0.0, **kw):
Frame.__init__(self, master, bg='white', width=350,height=20)
self.configure(**kw)
self._c = Tkinter.Canvas(self, bg=self['bg'],width=self['width'], height=self['height'],highlightthickness=0, relief='flat',bd=0)
self._c.grid(sticky=E+W)
self._r = self._c.create_rectangle(0, 0, 0,int(self['height']), fill=fillcolor, width=0)
self._t = self._c.create_text(int(self['width'])/2,int(self['height'])/2, text='')
self.set(value, text)
def set(self, value=0.0, text=None):
#make the value failsafe:
if value < 0.0:
value = 0.0
elif value > 1.0:
value = 1.0
if text == None:
#if no text is specified get the default percentage
text = str(int(round(100 * value))) + ' %'
self._c.coords(self._r, 0, 0, int(self['width']) * value,int(self['height']))
self._c.itemconfigure(self._t, text=text)
class App(Frame):
def __init__(self,parent=None, **kw):
Frame.__init__(self,parent,kw)
self.lblProgress = Label(self,text="Battery")
self.lblProgress.grid(row=0,column=0,padx=5,pady=5)
self.m = Meter(self,text='Hello')
self.m.grid(row=0,column=1,padx=5,pady=5)
self.m.set(0.0,'Hello')
self.lblProgress1 = Label(self,text="Altitude")
self.lblProgress1.grid(row=1,column=0,padx=5,pady=5)
self.m1 = Meter(self,text='Hello')
self.m1.grid(row=1,column=1,padx=5,pady=5)
self.m1.set(0.0)
self.value = 0.0
self._update()
def _update(self):
self.value += 0.01
if self.value > 0.99:
self.value = 0.0
altitude = str(self.value * 300) + " feet"
self.m.set(1.0-self.value)
self.m1.set(self.value-0.10,altitude)
self._timer = self.after(100, self._update)
def main():
root = Tk()
app = App(root)
app.grid(row=0)
root.mainloop()
if __name__ == '__main__':
main()