Verwenden von globalen Variablen
Verfasst: Samstag 6. Oktober 2018, 22:23
Hallo zusammen,
ich habe mir in mühevoller Kleinarbeit mein Programm am für den Raspberry zusammengestoppelt, jetzt hänge ich aber leider.
Ich möchte meine programmierte Anzeigetafel mit globalen Variablen versehen, damit ich diese dann auf einer 7-Segmentanzeige ausgeben kann. Aber irgendwie blick ich bei der Definition der Variablen nicht durch, bzw. weiß ich nicht wie und wo ich diese richtig definiere.
Hier mal mein Programm:
# -*- coding: cp1252 -*-
from Tkinter import *
import time
from datetime import datetime
from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi, noop
from luma.core.virtual import viewport, sevensegment
class StopWatch(Frame):
""" Implements a stop watch frame widget. """
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self._start = 0.0
self._elapsedtime = 0.0
self._running = 0
self.timestr = StringVar()
self.makeWidgets()
def makeWidgets(self):
""" Make the time label. """
l = Label(self, textvariable=self.timestr)
self._setTime(self._elapsedtime)
l.config(fg='Red', font=('Arial',100))
l.pack(fill=X, expand=NO, pady=2, padx=2)
def _update(self):
""" Update the label with elapsed time. """
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._timer = self.after(50, self._update)
def _setTime(self, elap):
""" Set the time string to Minutes:Seconds:Hundreths """
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
self.timestr.set('%02d:%02d' % (minutes, seconds))
def Start(self):
""" Start the stopwatch, ignore if running. """
if not self._running:
self._start = time.time() - self._elapsedtime
self._update()
self._running = 1
def Stop(self):
""" Stop the stopwatch, ignore if stopped. """
if self._running:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._running = 0
def Halbzeit(self):
""" Stop the stopwatch, ignore if stopped. """
self._start = time.time()
self._elapsedtime = 2700.00
self._setTime(self._elapsedtime)
def Reset(self):
""" Reset the stopwatch. """
self._start = time.time()
self._elapsedtime = 0.0
self._setTime(self._elapsedtime)
class CounterHome(Frame):
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self.counterh = 0
self.counter_label = Label(self, font="Arial 100 bold", fg="RED")
self.counter_label.pack()
Button(self, font=('Arial',30), width=10, text='Heim +', command=self.count_up).pack()
Button(self, font=('Arial',30), width=10, text='Heim -', command=self.count_down).pack()
self._update_counter()
def _update_counter(self):
self.counter_label['text'] = str(self.counterh)
def count_up(self):
self.counterh += 1
if self.counterh > 99 : self.counterh = 0
self._update_counter()
def count_down(self):
self.counterh -= 1
if self.counterh < 0 : self.counterh = 0
self._update_counter()
class CounterAway(Frame):
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self.countera = 0
self.counter_label = Label(self, font="Arial 100 bold", fg="RED")
self.counter_label.pack()
Button(self, font=('Arial',30), width=10, text='Auswaerts +', command=self.count_up).pack()
Button(self, font=('Arial',30), width=10, text='Auswaerts -', command=self.count_down).pack()
self._update_counter()
def _update_counter(self):
self.counter_label['text'] = str(self.countera)
def count_up(self):
self.countera += 1
if self.countera > 99 : self.countera = 0
self._update_counter()
def count_down(self):
self.countera -= 1
if self.countera < 0 : self.countera = 0
self._update_counter()
def main():
root = Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
"""root.geometry("%dx%d+0+0" % (w, h))"""
root.geometry('1000x1000')
sw = StopWatch(root)
sw.pack(side=TOP)
counterhome = CounterHome(root)
counterhome.place(x=200, y=300)
counteraway = CounterAway(root)
counteraway.place(x=450, y=300)
Button(root, width=7, font=('Arial',30), text='Start', command=sw.Start).place(x=50, y=200)
Button(root, width=7, font=('Arial',30), text='Stop', command=sw.Stop).place(x=300, y=200)
Button(root, width=7, font=('Arial',30), text='Halbzeit', command=sw.Halbzeit).place(x=800, y=200)
Button(root, width=7, font=('Arial',30), text='Reset', command=sw.Reset).place(x=550, y=200)
Button(root, width=10, font=('Arial',30), text='Quit', command=root.destroy).place(x=10, y=10)
"""
print home
print away
print time
hier sollte man die von oben aktuellen Werte als globale Variable zur Verfügung haben
"""
root.mainloop()
if __name__ == '__main__':
main()
Es macht nichts anderes als eine Uhr zu starten, stoppen, reseten und wenn man möchte auf Halbzeit zu springen.
Des weiteren kann der Spielstand auf- und abgezählt werden.
Damit ich diese Werte jetzt mit einem MAX7219 auf eine 8x7 Segmentanzeige schieben kann, bräuchte ich diese Werte global.
Kann mir vielleicht jemand einen Denkanstoss geben, wie und wo ich das definieren muss, damit das funzt?
Vielen Dank schon mal im Voraus,
Gruß Robert
ich habe mir in mühevoller Kleinarbeit mein Programm am für den Raspberry zusammengestoppelt, jetzt hänge ich aber leider.
Ich möchte meine programmierte Anzeigetafel mit globalen Variablen versehen, damit ich diese dann auf einer 7-Segmentanzeige ausgeben kann. Aber irgendwie blick ich bei der Definition der Variablen nicht durch, bzw. weiß ich nicht wie und wo ich diese richtig definiere.
Hier mal mein Programm:
# -*- coding: cp1252 -*-
from Tkinter import *
import time
from datetime import datetime
from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi, noop
from luma.core.virtual import viewport, sevensegment
class StopWatch(Frame):
""" Implements a stop watch frame widget. """
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self._start = 0.0
self._elapsedtime = 0.0
self._running = 0
self.timestr = StringVar()
self.makeWidgets()
def makeWidgets(self):
""" Make the time label. """
l = Label(self, textvariable=self.timestr)
self._setTime(self._elapsedtime)
l.config(fg='Red', font=('Arial',100))
l.pack(fill=X, expand=NO, pady=2, padx=2)
def _update(self):
""" Update the label with elapsed time. """
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._timer = self.after(50, self._update)
def _setTime(self, elap):
""" Set the time string to Minutes:Seconds:Hundreths """
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
self.timestr.set('%02d:%02d' % (minutes, seconds))
def Start(self):
""" Start the stopwatch, ignore if running. """
if not self._running:
self._start = time.time() - self._elapsedtime
self._update()
self._running = 1
def Stop(self):
""" Stop the stopwatch, ignore if stopped. """
if self._running:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._running = 0
def Halbzeit(self):
""" Stop the stopwatch, ignore if stopped. """
self._start = time.time()
self._elapsedtime = 2700.00
self._setTime(self._elapsedtime)
def Reset(self):
""" Reset the stopwatch. """
self._start = time.time()
self._elapsedtime = 0.0
self._setTime(self._elapsedtime)
class CounterHome(Frame):
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self.counterh = 0
self.counter_label = Label(self, font="Arial 100 bold", fg="RED")
self.counter_label.pack()
Button(self, font=('Arial',30), width=10, text='Heim +', command=self.count_up).pack()
Button(self, font=('Arial',30), width=10, text='Heim -', command=self.count_down).pack()
self._update_counter()
def _update_counter(self):
self.counter_label['text'] = str(self.counterh)
def count_up(self):
self.counterh += 1
if self.counterh > 99 : self.counterh = 0
self._update_counter()
def count_down(self):
self.counterh -= 1
if self.counterh < 0 : self.counterh = 0
self._update_counter()
class CounterAway(Frame):
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self.countera = 0
self.counter_label = Label(self, font="Arial 100 bold", fg="RED")
self.counter_label.pack()
Button(self, font=('Arial',30), width=10, text='Auswaerts +', command=self.count_up).pack()
Button(self, font=('Arial',30), width=10, text='Auswaerts -', command=self.count_down).pack()
self._update_counter()
def _update_counter(self):
self.counter_label['text'] = str(self.countera)
def count_up(self):
self.countera += 1
if self.countera > 99 : self.countera = 0
self._update_counter()
def count_down(self):
self.countera -= 1
if self.countera < 0 : self.countera = 0
self._update_counter()
def main():
root = Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
"""root.geometry("%dx%d+0+0" % (w, h))"""
root.geometry('1000x1000')
sw = StopWatch(root)
sw.pack(side=TOP)
counterhome = CounterHome(root)
counterhome.place(x=200, y=300)
counteraway = CounterAway(root)
counteraway.place(x=450, y=300)
Button(root, width=7, font=('Arial',30), text='Start', command=sw.Start).place(x=50, y=200)
Button(root, width=7, font=('Arial',30), text='Stop', command=sw.Stop).place(x=300, y=200)
Button(root, width=7, font=('Arial',30), text='Halbzeit', command=sw.Halbzeit).place(x=800, y=200)
Button(root, width=7, font=('Arial',30), text='Reset', command=sw.Reset).place(x=550, y=200)
Button(root, width=10, font=('Arial',30), text='Quit', command=root.destroy).place(x=10, y=10)
"""
print home
print away
print time
hier sollte man die von oben aktuellen Werte als globale Variable zur Verfügung haben
"""
root.mainloop()
if __name__ == '__main__':
main()
Es macht nichts anderes als eine Uhr zu starten, stoppen, reseten und wenn man möchte auf Halbzeit zu springen.
Des weiteren kann der Spielstand auf- und abgezählt werden.
Damit ich diese Werte jetzt mit einem MAX7219 auf eine 8x7 Segmentanzeige schieben kann, bräuchte ich diese Werte global.
Kann mir vielleicht jemand einen Denkanstoss geben, wie und wo ich das definieren muss, damit das funzt?
Vielen Dank schon mal im Voraus,
Gruß Robert