Hallo...
Es geht mal wieder um die GUI meiner Maschine, im Detail: um die grafische Darstellung der Spannungen als Balken..
Hierfür hatte meigrafd vor kurzem eine Klasse geschrieben, die allein für sich auch recht gut funktioniert..
In der Zwischenzeit hab ich es dann geschafft das die GUI im Vollbild ausgeführt wird & da schlecht killbar auf dem Main Reiter (auch tab genannt) nen Button zum beenden hinzugefügt..
Der Quelltext von Meigrafd:
#!/usr/bin/env python3
#
# Creator: meigrafd
# Copyright (C) 2017 by meiraspi@gmail.com published under the Creative Commons License (BY-NC-SA)
#
# https://www.forum-raspberrypi.de/Thread-python-gui-spannung-als-balken-anzeigen
# https://forum-raspberrypi.de/forum/thread/33903-re-gui-spannung-als-balken-anzeigen/
#
import tkinter as tk
class BarGraph(object):
def __init__(self, canvas, x=0, y=0, width=20, height=None, color='red',
outline='black', title=False, title_align=tk.SW, tags='',
value=0):
self.canvas = canvas
# http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/anchors.html
self.title_align = title_align
self.title = title
self.tags = tags
# http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/coordinates.html
self.x = x # Top left to right coordinate
self.y = y # Top left to bottom coordinate
self.background_color = color
self.border_color = outline
self.font = ('', 6)
self.width = width
self.height = height
self.canvas_width = canvas.winfo_width()
self.canvas_height = canvas.winfo_height()
if self.height is None:
self.height = self.canvas_height
self.bar, self.bar_title = None, None
self.value = value
self.set_value(value)
def set_value(self, value):
self.value = value
if self.title and not isinstance(self.title, str):
self.title = value
def value_color(self, value):
if value >= 90:
return "#FF0000"
elif value >= 70 and value <= 89:
return "#FF4000"
elif value >= 60 and value <= 69:
return "#FF8000"
elif value >= 50 and value <= 59:
return "#FFBF00"
elif value >= 30 and value <= 49:
return "#FFFF00"
elif value >= 20 and value <= 29:
return "#BFFF00"
elif value >= 10 and value <= 19:
return "#80FF00"
else:
return "#00FF00"
def update(self, value):
if self.bar is not None:
self.canvas.delete(self.bar)
if self.bar_title is not None:
self.canvas.delete(self.bar_title)
self.set_value(value)
self.draw()
def draw(self):
# Bottom left coordinate
if self.x == 0:
x0 = 0
else:
x0 = self.x + self.width
# Top left coordinates
y0 = self.height - self.value
# Bottom right coordinates
x1 = x0 + self.width
# Top right coordinates
y1 = self.height
# http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/create_rectangle.html
# Draw the bar
self.bar = self.canvas.create_rectangle(
x0, y0, x1, y1, fill=self.background_color,
outline=self.border_color)
# Put the title above the bar
if self.title:
self.bar_title = self.canvas.create_text(
x0 + 3, y0, angle="90", anchor=self.title_align, font=self.font,
text=str(self.title))
### Sample:
root = tk.Tk()
root.title("Bar Graph / Chart Bar")
root.geometry("600x300")
mainFrame = tk.Frame(root, relief=tk.SUNKEN, width=550, height=300)
mainFrame.pack()
c = tk.Canvas(master=mainFrame, width=500, height=250)
c.pack()
mainFrame.update()
b1 = BarGraph(canvas=c, color='yellow', value=189)
b1.draw()
b2 = BarGraph(canvas=c, color='blue', x=10, title=True)
b2.set_value(97)
b2.draw()
b2.update(113)
b3 = BarGraph(canvas=c, x=35, value=189, title='bla')
b3.draw()
b3.title='blub'
b3.update(157)
b4 = BarGraph(canvas=c, color='green', x=60, value=22, title=True)
b4.draw()
def update_scale_bar(event):
value = scale1.get()
b4.update(value)
b5 = BarGraph(canvas=c, x=90, value=120)
b5.draw()
scale1 = tk.Scale(mainFrame, from_=0, to=7, length=200, orient=tk.HORIZONTAL)
scale1.bind("<ButtonRelease-1>", update_scale_bar)
scale1.place(x=150, y=20)
root.mainloop()
# EOF
Alles anzeigen
Der Code meiner GUI mit meigrafd's klasse:
#!/usr/bin/env python3
import tkinter as tk
from tkinter import ttk
def quit():
global root
root.quit()
# meigrafd's BarGraph Class Copyright siehe orginal Quelltext..
class BarGraph(object):
def __init__(self, canvas, x=0, y=0, width=10, height=None, color='red',
outline='black', title=False, title_align=tk.SW, tags='',
value=0):
self.canvas = canvas
# http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/anchors.html
self.title_align = title_align
self.title = title
self.tags = tags
# http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/coordinates.html
self.x = x # Top left to right coordinate
self.y = y # Top left to bottom coordinate
self.background_color = color
self.border_color = outline
self.font = ('', 6)
self.width = width
self.height = height
self.canvas_width = canvas.winfo_width()
self.canvas_height = canvas.winfo_height()
if self.height is None:
self.height = self.canvas_height
self.bar, self.bar_title = None, None
self.value = value
self.set_value(value)
def set_value(self, value):
self.value = value
if self.title and not isinstance(self.title, str):
self.title = value
def value_color(self, value):
if value >= 90:
return "#FF0000"
elif value >= 70 and value <= 89:
return "#FF4000"
elif value >= 60 and value <= 69:
return "#FF8000"
elif value >= 50 and value <= 59:
return "#FFBF00"
elif value >= 30 and value <= 49:
return "#FFFF00"
elif value >= 20 and value <= 29:
return "#BFFF00"
elif value >= 10 and value <= 19:
return "#80FF00"
else:
return "#00FF00"
def update(self, value):
if self.bar is not None:
self.canvas.delete(self.bar)
if self.bar_title is not None:
self.canvas.delete(self.bar_title)
self.set_value(value)
self.draw()
def draw(self):
# Bottom left coordinate
if self.x == 0:
x0 = 0
else:
x0 = self.x + self.width
# Top left coordinates
y0 = self.height - self.value
# Bottom right coordinates
x1 = x0 + self.width
# Top right coordinates
y1 = self.height
# http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/create_rectangle.html
# Draw the bar
self.bar = self.canvas.create_rectangle(
x0, y0, x1, y1, fill=self.background_color,
outline=self.border_color)
# Put the title above the bar
if self.title:
self.bar_title = self.canvas.create_text(
x0 + 3, y0, angle="90", anchor=self.title_align, font=self.font,
text=str(self.title))
# ende meigrafd's BarGraph Class
#import matplotlib.pyplot as pyplot
# import pandas as pandas
### variable für test
# einzelne Netzteilspannungen in Var definieren:
nt5v1 = 5.0 # 5V Netzteil 1
nt5v2 = 7.0 # 5V Netzteil 2
sys5v = 5.2 # 5V Systemspannung
nt24v1 = 24.0 # 24V Netzteil 1
nt24v2 = 27.0 # 24V Netzteil 2
sys24v = 24.3 # 24V Systemspannung
nt48v1 = 48.0 # 48V Netzteil 1
nt48v2 = 52.0 # 48V Netzteil 2
sys48v = 48.5 # 48V Systemspannung
data5v = [nt5v1, nt5v2, sys5v]
data24v = [nt24v1, nt24v2, sys24v]
data48v = [nt48v1, nt48v2, sys48v]
### ende variablen für test
root = tk.Tk()
root.wm_title("MK-Zero GUI")
root.minsize(300, 300)
# Vollbildmodus
root.overrideredirect(1)
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))
# Ende Vollbildmodus
s = ttk.Style()
s.configure('TNotebook', tabposition='sw') #'ne' as in compass direction
# Fensterbreite
window_width = root.winfo_screenwidth() - 20
# Fensterhöhe
window_height = root.winfo_screenheight() -20
box = ttk.Notebook(root, width=window_width, height=window_height)
tab1 = tk.Frame(root)
tab2 = tk.Frame(root)
tab3 = tk.Frame(root)
tab4 = tk.Frame(root)
box.add(tab1, text="Main")
box.add(tab2, text="Spannungen")
box.add(tab3, text="Temperaturen")
box.add(tab4, text="testTab")
####### begin tab Main
tk.Button(tab1, text="GUI verlassen", command=quit).pack()
####### ende Tab MAIN
####### begin Tab Spannungen
labelFrame = tk.Frame(tab2, height=200, bg="#c0c0c0")
labelFrame.grid(row=0, columnspan=3, padx=5, pady=10)
#labelFrame.pack(fill="both", expand="yes")
# elemente des Frames labelFrame
labelLabel = tk.Label(labelFrame, text="Netzteilspannungen" )
labelLabel.grid(row=0, padx=5, pady=3)
# ende elemente des Frames labelLabel
nt5v1Frame = tk.Frame(tab2, width = 200, height=500, bg="#c0c0c0")
nt5v1Frame.grid(row=1, column=0, padx=5, pady=3)
# elemente des Frames nt5v1Frame
nt5v1Label = tk.Label(nt5v1Frame, text="5V")
nt5v1Label.grid(row=0, column=0, padx=5, pady=3)
#### begin test
c = tk.Canvas(master=nt5v1Label, width=50, height=400)
c.pack()
#nt5v1Label.update()
b1 = BarGraph(canvas=c, color='yellow', value=150, title=True)
b1.draw()
b2 = BarGraph(canvas=c, color='blue', x=10, title=True)
b2.set_value(97)
b2.draw()
b2.update(113)
b3 = BarGraph(canvas=c, x=35, value=189, title='bla')
b3.draw()
nt5v1Label.update()
# ende elemente nt5v1Frame
nt24v1Frame = tk.Frame(tab2, width=200, height=250, bg="#c0c0c0")
nt24v1Frame.grid(row=1, column=1, padx=5, pady=3)
# begin elemente nt24v1Frame
nt24v1Label = tk.Label(nt24v1Frame, text="24V")
nt24v1Label.grid(row=0, column=0, padx=5, pady=3)
# ende elemente nt24v1Frame
nt48v1Frame = tk.Frame(tab2, width=200, height=250, bg="#c0c0c0")
nt48v1Frame.grid(row=1, column=2, padx=5, pady=3)
# begin elemente nt48v1Frame
nt48v1Label = tk.Label(nt48v1Frame, text="48V")
nt48v1Label.grid(row=0, column=0, padx=5, pady=3)
# ende elemente nt48v1Frame
####### ende TAB Spannungen
####### begin TAB Temperaturen
#tempLabelFrame = tk.labelFrame(tab3, height=50, bg="#0000ff", text="This is a labelFrame")
#tempLabelFrame.grid(row=0, columnspan=3, padx=5, pady=3)
####### ende TAB Temperaturen
####### begin TestTAB
#var = dataset.groupby('Funktion').Umsatz.sum()
#fig = pyplot.figure()
#ax = fig.add_subplot(1,1,1)
#ax.set_xlabel('Funktion')
#ax.set_ylabel('Umsatz in Summe')
#ax.set_title('Umsatzvolumen nach Funktion der Filialen')
#var.plot(kind='bar')
#pyplot.show()
####### ende TestTAB
box.pack(side=tk.TOP)
root.mainloop()
Alles anzeigen
Und hier das Endergebnis:
Problem 1: vom LabelFrame fehlt auf einmal das Label... da sollte eigentlich "5V" stehen..
Problem 2: es werden keine Balken gezeichnet... Es erscheinen nur kleine waagerechte Striche nähe des oberen Rand des canvas..
Was mach ich falsch??
LG Tiieto