Home > Blockchain >  How to contact a specific label and change one of its programs (for example its bg). Instead of prod
How to contact a specific label and change one of its programs (for example its bg). Instead of prod

Time:09-17

from tkinter import *

class Information():

    def __init__(self, master):
        self.master = master
        self.load_dict_label()
        self.display()

    def display(self):
        for i in self.location_and_condition_label:
            bg_to_label = self.location_and_condition_label[i]["color"]
            row_to_label = self.location_and_condition_label[i]["row"]
            column_to_label = self.location_and_condition_label[i]["column"]
            text_to_label = self.location_and_condition_label[i]["text"]
            
            self.i = Label(self.master, font=("Courier", 14), text= text_to_label, bg=bg_to_label).grid(row=row_to_label, column=column_to_label, padx=5, pady=5)

For the example I chose random values.

def load_dict_label(self):
    color = "red"
    self.location_and_condition_label = {
        "0000": {"column": 0, "row": 0, "text": "5:00", "color": color},
        "0001": {"column": 1, "row": 0, "text": "Getting up", "color": color},
        "0002": {"column": 2, "row": 0, "text": "Wash face", "color": color},
        "0003": {"column": 3, "row": 0, "text": "Get dress", "color": color},
        "0004": {"column": 0, "row": 1, "text": "6:00", "color": color},
        "0005": {"column": 1, "row": 1, "text": "Drink coffee", "color": color},
        "0006": {"column": 2, "row": 1, "text": "Read  newspaper", "color": color},
        "0007": {"column": 0, "row": 2, "text": "7:00", "color": color},
        "0008": {"column": 1, "row": 2, "text": "Turn on tv", "color": color},
        "0009": {"column": 2, "row": 2, "text": "Drink coffee", "color": color},
        "0010": {"column": 3, "row": 2, "text": "Wake the child", "color": color},
        "0011": {"column": 4, "row": 2, "text": "Organize the\n child", "color": color},
        "0012": {"column": 5, "row": 2, "text": "Make us \nbreakfast", "color": color},
        "0013": {"column": 0, "row": 3, "text": "8:00", "color": color},
        "0014": {"column": 1, "row": 3, "text": "To go \nto work", "color": color},
        "0015": {"column": 2, "row": 3, "text": "Say hello to\neveryone", "color": color},
        "0016": {"column": 0, "row": 4, "text": "9:00", "color": color},
        "0017": {"column": 1, "row": 4, "text": "Make a morning\ncall", "color": color},
        "0018": {"column": 2, "row": 4, "text": "Start working", "color": color},
        "0019": {"column": 3, "row": 4, "text": "Drink coffee", "color": color}
    }

This function I want to change that instead of producing a new one I want to update an old function and display on window.

    def update_label(self):
        self.location_and_condition_label["0010"]["color"] = "green"
        self.display()

class MainPanel():
    def __init__(self, master):
        self._master = master
        self._master.configure(background="#929591")
        self._nformation = Information(self._master)
        self.running = True

    def run(self):
        if self.running == True:
            self._nformation.update_label()

        self._master.after(5000, app.run)

if __name__ == "__main__":

    root = Tk()
    app = MainPanel(root)
    root.after(5000, app.run)

    root.mainloop()

CodePudding user response:

You can store the labels in a dictionary, and then use the configure method to change the color.

def display(self):
    for i in self.location_and_condition_label:
        ... 
        label = Label(self.master, font=("Courier", 14), text= text_to_label, bg=bg_to_label)
        label.grid(row=row_to_label, column=column_to_label, padx=5, pady=5)
        self.labels[i] = label

def update_label(self):
    self.location_and_condition_label["0002"]["color"] = "green"
    self.labels["0002"].configure(background="green")

Things to notice:

  • the creation of the label and calling grid are separate, so that label is set to the actual label widget
  • we add the label to a dictionary named self.labels
  • update_label does not need to call display, since display creates new labels each time it is called. Instead, we call the configure method on the specific label

Alternately, you could modify display to only create the widget if it doesn't exist, and then always use configure to update it to the current specification. The solution would then look something like this:

def __init__(self, master):
    self.master = master
    self.labels = {}  # initialize self.labels to an empty dictionary
    self.load_dict_label()
    self.display()

def display(self):
    for i in self.location_and_condition_label:
        ...
        if i not in self.labels:
            # create the label if it hasn't been created
            label = Label(self.master, font=("Courier", 14))
            label.grid(row=row_to_label, column=column_to_label, padx=5, pady=5)
            self.labels[i] = label

        # update the label with the stored configuration values
        self.labels[i].configure(
            text= text_to_label, bg=bg_to_label
        )

def update_label(self):
    self.location_and_condition_label["0002"]["color"] = "green"
    self.display()
  • Related