Я делаю графический интерфейс Python Tkinter, где у меня есть два класса, они называются temperatureWidget
и lightWidget
, оба из которых расширяют свой родительский элемент с именем Widget
. В родительском классе у меня есть Entry и кнопка, которая связана с функцией с именем setSensors()
. В этой функции я хочу проверить, какой подкласс используется. Так, например, если подкласс равен temperatureWidget
, то прочитайте ввод шкалы, называемой «TemperatureSlider», и поместите значение этого в метку, названную «Метка температуры». Это может быть очень расплывчато, но код прояснит
import random
# from random import *
from tkinter import *
import threading
from queue import *
import datetime
import matplotlib as matplotlib
root = Tk()
root.configure(background="white")
root.title("Project 2.1")
class SerialThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
class GUI:
def __init__(self, master):
# Random most secnificant bit tussen 0 en 1
self.msb = random.randint(0, 1)
# Lijst met widgets
self.widgets = []
# De root
self.master = master
# Zet het scherm volledige scherm
self.master.geometry("{0}x{1}+0+0".format(master.winfo_screenwidth(), master.winfo_screenheight()))
# Maak een nieuwe frame aan
self.widgetFrame = Frame(master, bg="white")
# En zet deze frame boven aan de pagina
self.widgetFrame.pack(side=TOP)
# Maak een nieuwe frame aan en zet deze in de widgetFrame
self.widgetTop = Frame(self.widgetFrame, bg="white")
# Zet deze frame bovenaan in de widgetframe
self.widgetTop.pack(side=TOP, fill=X)
#
self.removeText = Entry(self.widgetTop)
self.removeText.pack()
removeButton = Button(self.widgetTop, text="Remove widget ", command=self.removeWidget)
removeButton.pack()
addButton = Button(self.widgetTop, text="Add widget", command=self.addWidgetToScreen)
addButton.pack()
def showGui(self):
# Als de lengte van de lijst met widgets groter is dan 0
if len(self.widgets):
# Voor alle widgets in de lijst met widgets
for widget in range(len(self.widgets)):
# Stop de widget in de mainframe
self.widgets[widget].mainFrame.pack(side=LEFT, pady=20, padx=20, fill=Y)
# Anders
else:
# Laat deze label zien
NoInputLabel = Label(self.widgetTop, text="No modules connected, please connect a module",
font='Helvetica 16 bold', bg='white', anchor='center')
NoInputLabel.pack(side=TOP, pady=50, padx=20)
def addWidget(self, widget):
# Voeg de widget toe aan de lijst met widgets
self.widgets.append(widget)
def removeWidget(self):
# Voor alle widgets in de lijst met widgets
for index, widget in enumerate(self.widgets):
# Als de naam van de widget gelijk is aan de ingevulde naam
if widget.name == self.removeText.get():
# Verwijder dan die widget uit de mainframe
self.widgets[index].mainFrame.pack_forget()
# Em verwijder hem uit de lijst
del self.widgets[index]
def addWidgetToScreen(self):
# Als de lengte van de widget groter is dan 0
num = random.randint(0, 1)
if len(self.widgets):
if num == 0:
# En maak een nieuwe widget aan met als nummer één hoger dan de laatste
newWidget = temperatureWidget(self.master, "COM" + str(len(self.widgets) + 1), self)
else:
newWidget = lightWidget(self.master, "COM" + str(len(self.widgets) + 1), self)
else:
# Maak een widget aan met nummer 1
if num == 0:
# En maak een nieuwe widget aan met als nummer één hoger dan de laatste
newWidget = temperatureWidget(self.master, "COM1", self)
else:
newWidget = lightWidget(self.master, "COM1", self)
# Voeg hem dan toe aan de lijst met widgets
self.widgets.append(newWidget)
# Pak de widget in de mainframe
self.widgets[-1].mainFrame.pack(side=LEFT, pady=20, padx=20, fill=Y)
class Widget:
def __init__(self, master, name, gui):
self.msb = 0
self.master = master
self.name = name
self.gui = gui
# Set de main frame van een widget
self.mainFrame = Frame(gui.widgetFrame, bg='white')
# Maak een frame die de top van de mainframe hanteerd
self.mainFrameTop = Frame(self.mainFrame, bg='white')
self.mainFrameTop.pack(side=TOP)
# Maak een die onder de top frame komt en daarom center is
self.mainFrameCenter = Frame(self.mainFrame, bg='white')
self.mainFrameCenter.pack(side=TOP)
# Maak een die daar weer onder komt en dus onderstaan zit
self.mainFrameBottom = Frame(self.mainFrame, bg='white')
self.mainFrameBottom.pack(side=TOP)
colorArray = list(matplotlib.colors.cnames.values())
self.WidgetName = Label(self.mainFrameTop, text=name, font='Helvetica 16 bold', bg=random.choice(colorArray))
self.WidgetName.pack(fill=X)
self.msb = random.randint(0, 1)
self.maxRolloutPositionLabel = Label(self.mainFrameTop, text="Rolling distance (cm): ", bg="white")
self.maxRolloutPositionLabel.pack()
self.maxRolloutEntry = Entry(self.mainFrameTop, bg="white")
self.maxRolloutEntry.pack()
self.submitFormButton = Button(self.mainFrameTop, text="Submit", command=self.setSensors, bg="white")
self.submitFormButton.pack()
'''
Sunblind status functions and GUI elements
'''
self.openSunblindButton = Button(self.mainFrameCenter, text="Open sunblind", command=self.openSunblind,
state=NORMAL,
bg="#28a745", fg="white")
self.openSunblindButton.pack(side=LEFT)
self.closeSunblindButton = Button(self.mainFrameCenter, text="Close sunblind", command=self.closeSunblind,
state=NORMAL, bg="#dc3545", fg="white")
self.closeSunblindButton.pack(side=LEFT)
self.setSunblindStatusButton = Button(self.mainFrameCenter, text="Automatic", command=self.setSunblindStatus,
bg="#6c757d", fg="white")
self.setSunblindStatusButton.pack(side=LEFT)
self.sunblindFrame = Frame(self.mainFrameBottom, bg="white")
self.sunblindFrame.pack(fill=X)
self.sunblindStatusLabel = Label(self.sunblindFrame, text="Sunblind status:", bg="white", anchor="w")
self.sunblindStatusLabel.pack(side=LEFT, pady=5)
self.sunblindStatus = Label(self.sunblindFrame, text="Manual", bg="white")
self.sunblindStatus.pack(side=RIGHT, pady=5)
self.rolloutFrame = Frame(self.mainFrameBottom, bg="white")
self.rolloutFrame.pack(fill=X)
self.rolloutLabel = Label(self.rolloutFrame, text="Roll-out position: ", justify=LEFT, bg="white", anchor="w")
self.rolloutLabel.pack(side=LEFT, pady=5)
self.rolloutValue = Label(self.rolloutFrame, text="", bg="white")
self.rolloutValue.pack(side=RIGHT, pady=5)
self.variable = StringVar(self.mainFrameBottom)
self.variable.set(self.name)
self.chooseArduino = OptionMenu(self.mainFrameBottom, self.variable, "Living Room", "Bedroom", "Study")
self.chooseArduino.pack(side=LEFT)
self.setNameButton = Button(self.mainFrameBottom, text="Set name", command=self.setArduinoName)
self.setNameButton.pack(side=LEFT)
def setSensors(self):
print("Set the temperature and light of the sensors")
# Check which subclass is used
self.maxRolloutPositionLabel.config(text="Rolling distance (cm): " + str(self.maxRolloutEntry.get()))
def setName(self, widgetName):
self.name = widgetName
def openSunblind(self):
print("Set the sunblind to an open state")
self.rolloutValue.config(text="Rolled out")
def closeSunblind(self):
print("Set the sunblind to an closed state")
self.rolloutValue.config(text="Rolled in")
def setSunblindStatus(self):
if self.setSunblindStatusButton.config('text')[-1] == 'Automatic':
self.openSunblindButton.config(state=DISABLED)
self.closeSunblindButton.config(state=DISABLED)
self.setSunblindStatusButton.config(text='Manual')
print("Sunblind is set to: " + self.setSunblindStatusButton['text'])
self.sunblindStatus.config(text="Automatic")
self.rolloutValue.config(text='')
else:
self.openSunblindButton.config(state=NORMAL)
self.closeSunblindButton.config(state=NORMAL)
self.setSunblindStatusButton.config(text='Automatic')
print("Sunblind is set to: " + self.setSunblindStatusButton['text'])
self.sunblindStatus.config(text="Manual")
def setArduinoName(self):
self.WidgetName.config(text=self.variable.get())
def getTemperature(self):
return self.temperature
def getLightIntensity(self):
return self.lightIntensity
class temperatureWidget(Widget):
def __init__(self, master, name, gui):
super().__init__(master, name, gui)
self.temperatureSliderLabel = Label(self.mainFrameTop, text="Temperature (°C)", bg="white")
self.temperatureSlider = Scale(self.mainFrameTop, orient=HORIZONTAL, length=250, from_=0, to=40, bg="white")
self.temperatureSliderLabel.pack()
self.temperatureSlider.pack()
self.temperatureFrame = Frame(self.mainFrameBottom, bg="white")
self.temperatureFrame.pack(fill=X)
self.temperatureValueLabel = Label(self.temperatureFrame, text="Temperature: ", justify=LEFT, bg="white",
anchor="w")
self.temperatureValue = Label(self.temperatureFrame, text="", bg="white")
self.temperatureValueLabel.pack(side=LEFT, pady=5)
self.temperatureValue.pack(side=RIGHT, pady=5)
class lightWidget(Widget):
def __init__(self, master, name, gui):
super().__init__(master, name, gui)
self.lightSliderLabel = Label(self.mainFrameTop, text="Light Intensity (%)", bg="white")
self.lightSlider = Scale(self.mainFrameTop, orient=HORIZONTAL, length=250, bg="white")
self.lightSliderLabel.pack()
self.lightSlider.pack()
self.lightSliderFrame = Frame(self.mainFrameBottom, bg="white")
self.lightSliderFrame.pack(fill=X)
self.lightSliderValueLabel = Label(self.lightSliderFrame, text="Light Intensity: ", justify=LEFT,
bg="white",
anchor="w")
self.lightSliderValue = Label(self.lightSliderFrame, text="", bg="white")
self.lightSliderValueLabel.pack(side=LEFT, pady=5, fill=X)
self.lightSliderValue.pack(side=RIGHT, pady=5)
Visueel = GUI(root)
Arduino1 = lightWidget(root, "COM1", Visueel)
Arduino2 = temperatureWidget(root, "COMT", Visueel)
Arduino3 = lightWidget(root, "COML", Visueel)
Arduino4 = temperatureWidget(root, "COM4", Visueel)
Arduino5 = lightWidget(root, "COM5", Visueel)
Visueel.addWidget(Arduino1)
Visueel.addWidget(Arduino2)
Visueel.addWidget(Arduino3)
Visueel.addWidget(Arduino4)
Visueel.addWidget(Arduino5)
Visueel.showGui()
root.mainloop()
Я искал ответ более часа, но, похоже, не нашел его. Возможно ли это?