Ich möchte mir eine GUI bauen, diese hat mehrere Oberfläche durch die man durch schalten kann.
Nun habe ich das mit dem wechseln von Oberflächen nicht so ganz verstanden, vielleicht ist mein Ansatz auch falsch.
Ich habe eine Oberfläche, diese möchte ich löschen und daraufhin dann die neue laden. Ist das richtig so?
Hier mein Code
Code: Alles auswählen
#!/usr/bin/env python
import Tkinter
from Tkinter import *
textFont = "Helvetica"
top = Tkinter.Tk()
activeWindow = "BOOT"
C = Tkinter.Canvas(top, bg="black", height=480, width=320)
# overview.overrideredirect(1)
# overview.focus_set()
# top.config(cursor="none")
# Declare Variables
measuredItems = ["RPM", "Water", "EGT", "Fuel Cap.", "Fuel Flow", "Oil Temp", ]
measuredItemsColor = ["green", "green", "green", "green", "green", "green"]
def drawDigitalGaugeTK(xval, yval, color, value, maxVal, name, gaugeScale):
coord = xval + 5, yval + 5, (xval + 100) * gaugeScale, (yval + 100) * gaugeScale
gaugeValue = maxVal / float(value)
gauge = C.create_arc(xval, yval, (xval + 100 * gaugeScale), (yval + 100 * gaugeScale), start=0,
extent=-(220 / gaugeValue), fill=color) # Draw hand
outline = C.create_arc(xval - 3, yval - 3, (xval + 100 * gaugeScale + 3), (yval + 100 * gaugeScale + 3), start=0,
extent=-220, style="arc", outline="white", width=2) # draw outline
valueBox = C.create_rectangle((xval + 50 * gaugeScale), yval + 20 * gaugeScale, xval + 100 * gaugeScale + 3,
yval + 50 * gaugeScale, outline='white', width=2) # draw Value Box
value = Label(top, text=value, bg="black", fg="turquoise", font=(textFont, int(round(14 * gaugeScale)))).place(
x=xval + 52 * gaugeScale, y=yval + 20 * gaugeScale) # Write value into the box
value = Label(top, text=name, bg="black", fg="white", font=(textFont, int(round(20 * gaugeScale)))).place(x=xval,
y=yval - 15) # Write value into the box
def drawBandGraph():
pass
def drawButtons():
# left
Button(top, text="MAIN", wraplength=1, command=lambda: setActiveWindow("Main"), pady=6, font=(textFont, 10)).place(
x=0, y=0)
Button(top, text="ENG", wraplength=1, command=lambda: setActiveWindow("ENG"), pady=6, font=(textFont, 10)).place(
x=0, y=120)
Button(top, text="NAV", wraplength=1, command=lambda: setActiveWindow("NAV"), pady=6, font=(textFont, 10)).place(
x=0, y=240)
Button(top, text="STAT", wraplength=1, command=lambda: setActiveWindow("STAT"), pady=6, font=(textFont, 10)).place(
x=0, y=360)
# right
Button(top, text="PERF.", wraplength=1, command=lambda: setActiveWindow("PERFORMANCE"), pady=6,
font=(textFont, 10)).place(x=290, y=0)
Button(top, text="MEDIA", wraplength=1, command=lambda: setActiveWindow("MEDIA"), pady=6,
font=(textFont, 10)).place(x=290, y=120)
Button(top, text="NET", wraplength=1, command=lambda: setActiveWindow("NETWORK"), pady=6,
font=(textFont, 10)).place(x=290, y=240)
Button(top, text="SETTINGS", wraplength=1, command=lambda: setActiveWindow("BOOT"), pady=6,
font=(textFont, 10)).place(x=290, y=360)
def errorWindow(): # width 120px ; height 200px
xval = 190 # Xval the Error Box is drawn
yval = 0 # Yval the Error Box is drawn
itemlength = len(measuredItemsColor)
itemfound = 0
for i in range(0, itemlength):
if (measuredItemsColor[i] == "yellow" or measuredItemsColor[i] == "red"):
itemfound += 1
value = Label(top, text=measuredItems[i], bg="black", fg=measuredItemsColor[i], font=(textFont, 20)).place(
x=xval + 3, y=yval + 3 + (25 * (itemfound - 1))) # Write value into the box
def bootScreen():
drawDigitalGaugeTK(70, 50, measuredItemsColor[0], 2000, 6000, measuredItems[0], 0.8)
def mainWindow():
drawDigitalGaugeTK(50, 20, measuredItemsColor[0], 2000, 6000, measuredItems[0], 0.8)
drawDigitalGaugeTK(50, 120, measuredItemsColor[1], 2000, 6000, measuredItems[1], 0.8)
drawDigitalGaugeTK(50, 220, measuredItemsColor[2], 2000, 6000, measuredItems[2], 0.8)
def setActiveWindow(windowName):
global oldWindow
oldWindow = ""
global activeWindow
activeWindow = windowName
def loadActiveWindow():
if (activeWindow != oldWindow):
C.delete("all")
if (activeWindow == "MAIN"):
mainWindow()
if (activeWindow == "BOOT"):
bootScreen()
activeWindowOld = activeWindow
errorWindow()
C.pack()
# top.mainloop()
setActiveWindow("MAIN")
drawButtons() # Draws Main Buttons
while True:
loadActiveWindow()
top.update()
# time.sleep(0.2)
Leider löscht es nur basics wie Arcs oder Rechtecke auf dem Canvas. Dinge wie Labels oder Butons bleiben jedoch erhalten.
Wie ist hier die richtige vorgehensweise?