У меня есть проект, в котором у меня есть несколько «виджетов» в кадре. Я передаю список виджетов в класс с именем GUI, который отображает все эти «виджеты». У меня есть кнопка на моем фрейме, которая должна удалить один из виджетов из списка. Который это делает. Но сам виджет не исчезает из фрейма.
Так что для уточнения
В классе GUI есть список объектов [Object, Object, Object, Object]
. Теперь он отображает 4 объекта виджета. Когда я нажимаю кнопку удаления, новый список выглядит как [Object, Object, Object]
, но список в классе GUI по-прежнему [Object, Object, Object, Object]
. Как я могу сделать так, чтобы графический интерфейс всегда использовал самую новую версию списка?
Код для уточнения
import random
from tkinter import *
import datetime
root = Tk()
root.configure(background="white")
root.title("Project 2.1")
Widgets = []
class GUI:
def __init__(self, widgets, master):
self.widgets = widgets
self.master = master
self.master.geometry("{0}x{1}+0+0".format(master.winfo_screenwidth(), master.winfo_screenheight()))
for widget in range(len(widgets)):
widgets[widget].widgetFrame.pack(side=LEFT, pady=20, padx=20, fill=Y)
if len(widgets) == 0:
NoInputLabel = Label(master, text="No modules connected, please connect a module", font='Helvetica 16 bold')
NoInputLabel.pack(side=TOP)
removeButton = Button(master, text="Remove widget", command=self.removeWidget)
removeButton.pack()
print(self.widgets)
def removeWidget(self):
self.widgets = self.widgets[:-1]
print(self.widgets)
class Widget:
def __init__(self, master, name):
self.master = master
self.name = name
colorArray = ["#e3e0f3", "#eebddd", "#80a3db", "#036fa0"]
self.widgetFrame = Frame(master, bg="white")
# widgetFrame.pack(side=LEFT, pady=20, padx=50)
widgetTop = Frame(self.widgetFrame, bg="white")
widgetTop.pack(side=TOP)
widgetCenter = Frame(self.widgetFrame, bg="white")
widgetCenter.pack(side=TOP)
widgetBottom = Frame(self.widgetFrame, bg="white")
widgetBottom.pack(side=TOP, fill=X)
self.WidgetName = Label(widgetTop, text=name, font='Helvetica 16 bold', bg=random.choice(colorArray))
self.WidgetName.pack(fill=X)
self.temperatureSliderLabel = Label(widgetTop, text="Temperature (°C)", bg="white")
self.temperatureSliderLabel.pack()
self.temperatureSlider = Scale(widgetTop, orient=HORIZONTAL, length=250, from_=0, to=40, bg="white")
self.temperatureSlider.pack()
self.lightSliderLabel = Label(widgetTop, text="Light Intensity (%)", bg="white")
self.lightSliderLabel.pack()
self.lightSlider = Scale(widgetTop, orient=HORIZONTAL, length=250, bg="white")
self.lightSlider.pack()
self.maxRolloutPositionLabel = Label(widgetTop, text="Rolling distance (cm): ", bg="white")
self.maxRolloutPositionLabel.pack()
self.maxRolloutEntry = Entry(widgetTop, bg="white")
self.maxRolloutEntry.pack(side=LEFT)
self.submitFormButton = Button(widgetTop, text="Submit", command=self.setSensors, bg="white")
self.submitFormButton.pack(side=LEFT)
self.openSunblindButton = Button(widgetCenter, text="Open sunblind", command=self.openSunblind, state=NORMAL,
bg="#28a745", fg="white")
self.openSunblindButton.pack(side=LEFT)
self.closeSunblindButton = Button(widgetCenter, text="Close sunblind", command=self.closeSunblind,
state=NORMAL, bg="#dc3545", fg="white")
self.closeSunblindButton.pack(side=LEFT)
self.setSunblindStatusButton = Button(widgetCenter, text="Automatic", command=self.setSunblindStatus,
bg="#6c757d", fg="white")
self.setSunblindStatusButton.pack(side=LEFT)
self.sunblindFrame = Frame(widgetBottom, 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.temperatureFrame = Frame(widgetBottom, bg="white")
self.temperatureFrame.pack(fill=X)
self.temperatureValueLabel = Label(self.temperatureFrame, text="Temperature: ", justify=LEFT, bg="white",
anchor="w")
self.temperatureValueLabel.pack(side=LEFT, pady=5)
self.temperatureValue = Label(self.temperatureFrame, text="", bg="white")
self.temperatureValue.pack(side=RIGHT, pady=5)
self.lightSliderFrame = Frame(widgetBottom, bg="white")
self.lightSliderFrame.pack(fill=X)
self.lightSliderValueLabel = Label(self.lightSliderFrame, text="Light Intensity: ", justify=LEFT, bg="white",
anchor="w")
self.lightSliderValueLabel.pack(side=LEFT, pady=5)
self.lightSliderValue = Label(self.lightSliderFrame, text="", bg="white")
self.lightSliderValue.pack(side=RIGHT, pady=5)
self.rolloutFrame = Frame(widgetBottom, 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(widgetBottom)
self.variable.set(self.name)
self.chooseArduino = OptionMenu(widgetBottom, self.variable, "Living Room", "Bedroom", "Study", "Sex Dungeon",
"Bingo club")
self.chooseArduino.pack()
self.setNameButton = Button(widgetBottom, text="Set name", command=self.setArduinoName)
self.setNameButton.pack()
def setSensors(self):
print("Set the temperature and light of the sensors")
self.temperatureValue.config(text=str(self.temperatureSlider.get()) + "°C")
self.temperature = self.temperatureSlider.get()
self.lightSliderValue.config(text=str(self.lightSlider.get()))
self.lightIntensity = self.lightSlider.get()
self.maxRolloutPositionLabel.config(text="Rolling distance (cm): " + str(self.maxRolloutEntry.get()))
def setName(self, widgetName):
self.widgetName = 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")
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
Arduino1 = Widget(root, "Arduino 1")
Arduino2 = Widget(root, "Arduino 2")
Arduino3 = Widget(root, "Arduino 3")
Arduino4 = Widget(root, "Arduino 4")
Arduino5 = Widget(root, "Arduino 5")
Widgets.append(Arduino1)
Widgets.append(Arduino2)
Widgets.append(Arduino3)
Widgets.append(Arduino4)
Widgets.append(Arduino5)
Visueel = GUI(Widgets, root)
root.mainloop()
Изображение