#class LabelHandler

1 messages · Page 1 of 1 (latest)

whole fjord
#

The general approach would be as follows:

class LabelHandler:
    def __init__(self): # besides self you also put any data that should be needed when the class is created.
        self.labels = [] # init an empty List for now. You may also use Dict, if you need to address labels by name

    def addLabel(self):
        label = label.Label() # whatever the logic you are using to create the label
        self.labels.append(label) # add the label

    def clearLabels(self):
        for label in self.labels:
            pass # do whatever you need to do to remove individual labels
        self.labels = [] # set to an empty
#

class LabelHandler

#

If you want more detailed instructions, let me know and provide the code you need to implement.

coarse solar
#

Also, the question would be if you want to create a label class (so that it can remember its text) or a handler to just show and destroy

whole fjord
coarse solar
#

No, a class for the label itself. Looking at the code:

Cancel = label.Label(terminalio.FONT, text="Up to cancel", color=white, x=58, y=122)

This would be shown multiple times. Creating a class, so the global object would contain the needed information to easily create and destroy.

whole fjord
#

There's a lot of ways of making the code more scalable and readable, yeah.

tulip needle
#

I see this:
# init an empty List for now. You may also use Dict, if you need to address labels by name
What do I do to make my labels have names? Because I need to know each and every label name so I can set them to hidden or change their color or other things.

whole fjord
tulip needle
#

I've got this far, but how would I call that label name to change something like:

label = label.Label(terminalio.FONT, text = text, color = colorDefine(), x = x, y = y)

label.hidden = True
whole fjord
#
from adafruit_display_text.label import Label # import it like this, so you don't have variable collition

class LabelHandler:
    def __init__(self): # besides self you also put any data that should be needed when the class is created.
        self.labels = {}

    def addLabel(self):
        label = Label() # whatever the logic you are using to create the label
        self.labels['my_label'] = label # add the label

    def clearLabels(self):
        for label in self.labels:
            pass # do whatever you need to do to remove individual labels
        self.labels = [] # set to an empty


handler = LabelHandler()
handler.addLabel('Some text and stuff')
# then use access the labels by name like so
handler.labels['my_label'].hidden = true
#

I changed the import there. FYI

#

Oh and I forgot to modify the clearLabels command.

#

If you need that I can explain it, too.

coarse solar
#

Wouldn't you have add label also take in the label name? So you can add multiple?

whole fjord
#

If you create a list with all label data you could definitely create all of them using a loop.

#

Also, if you know that your labels won't change, you could just hardcode it and create all of them in the __init__() function.

#

Or at least have the data stored in there and create them by just running one function.

tulip needle
#

See where it's getting extremely confusing for me is that I have 40 labels currently getting used in my program, more to come. So now I don't even know where to begin to implement this class..

#

Every label obviously has different text, x, y, color, etc.

whole fjord
#

It doesn't matter where you implement it. I would even put it in a separate file. You import it in your main file, or before you start creating labels. then reference that one handler variable for all your needs.

tulip needle
#

But how does the class know that this:
handler.addLabel('Some text and stuff')
is the "my_label"?

coarse solar
#

Is each label static?

tulip needle
#

What do you mean by static?

whole fjord
#

You can keep your labels data from which you create labels in a list, like so:

label_templates = [
  ('label_nickname_1', 'Label text 1', 10, 10, 0xFF0000),
  ('label_nickname_2', 'Label text 2', 10, 10, 0xFF0000),
# and so on
]
coarse solar
#

Static as in doesn't change

coarse solar
#

For that it would be: handler.addLabel('label_1', 'label text')

tulip needle
whole fjord
tulip needle
#

Sorry if I'm getting confusing. I'm getting frustrated with myself because there's literally SO many different things going on with these labels.

whole fjord
# tulip needle Look, here is my code..

Can the microcontroller you're using handle that many labels?
Either way, you're not doing very efficiently. Lot's of hand-written/copied lines. You need to do more loops.

tulip needle
#

As of right now it's handleing those labels.

coarse solar
#

Stepping away, feel free to reach out directly if I can help later.

tulip needle
#

Okay

tulip needle
#

I think I have a very big issue with my knowledge of Python.. I honestly don't know how I learned anything in Python. Even though I wrote 95% of this code..

whole fjord
# tulip needle More loops for what specifcally? Like, I don't get it at all.

For example:

D4_text = label.Label(terminalio.FONT, text="D4", scale=diceScale, color=white, x=col1_variable, y=row1_variable)
D6_text = label.Label(terminalio.FONT, text="D6", scale=diceScale, color=white, x=col1_variable, y=row2_variable)
D8_text = label.Label(terminalio.FONT, text="D8", scale=diceScale, color=white, x=col1_variable, y=row3_variable)
#... 16 more lines

could be just a simple, like:

labels_info = [
    {"text": "D4", "x": col1_variable, "y": row1_variable},
    {"text": "D6", "x": col1_variable, "y": row2_variable},
    {"text": "D8", "x": col1_variable, "y": row3_variable},
    {"text": "D10", "x": col1_variable, "y": row4_variable},
    {"text": "D12", "x": col2_variable, "y": row1_variable},
    {"text": "D20", "x": col2_variable, "y": row2_variable},
    {"text": "D100", "x": col2_variable, "y": row3_variable},
    {"text": "%", "x": col2_variable, "y": row4_variable},
]

labels_dict = {}
highlights_dict = {}

for info in labels_info:
    labels_dict[info["text"]] = label.Label(terminalio.FONT, text=info["text"], scale=diceScale, color=white, x=info["x"], y=info["y"])

for info in labels_info:
    highlights_dict[info["text"]] = label.Label(terminalio.FONT, text=info["text"], background_color=colorDefine(), scale=diceScale, color=whiteGreyDefine(), x=info["x"], y=info["y"])

colorSelect_text = label.Label(terminalio.FONT, text="C", color=white, x=120, y=120)
#

You write one loop to create whatever number of labels you want

#

instead of copy-pasting it dozens of times. And making your file 1000 lines long.

#

Don't take what I wrote for direct usage, it's just an example.

tulip needle
whole fjord
#

Before you go any further, I suggest you refactor your code and make it more manageable.

#

The class approach that I showed you should help.

#

Feel free to ask very specific questions.

tulip needle
#

It's not helping a single bit.

#

:(

whole fjord
#

Well, I think the problem is that you don't fully understand what you write.

tulip needle
#

Maybe that's a good way to put it.

whole fjord
#

And it's hard to help you if you narrow down what exactly you have trouble with. I can't teach you the entirety of Python.

tulip needle
#

I get that

whole fjord
#

But I can help you find a direction to learn

#

The biggest issue with your file that I see is a lot of redundant code.

#

And that you use way to many variables. As I said, put then into a list or a dictionary

#

You also don't have to put all your labels into 1 dictionary. If you have labels that are usually worked on together, you can put then in a separate dictionary, so it's easier to loop over the values.

#

Do you understand what I mean?

tulip needle
#

I do but don't kind of?

#

I dunno.

whole fjord
#

Ok, I'll provide another practical example.

#

You have this function:

def colorUpdate(newColor):
    D4_highlight.color = whiteGreyDefineUpdate(newColor)
    D6_highlight.color = whiteGreyDefineUpdate(newColor)
    D8_highlight.color = whiteGreyDefineUpdate(newColor)
    D10_highlight.color = whiteGreyDefineUpdate(newColor)
    D12_highlight.color = whiteGreyDefineUpdate(newColor)
    D20_highlight.color = whiteGreyDefineUpdate(newColor)
    D100_highlight.color = whiteGreyDefineUpdate(newColor)
    perc_highlight.color = whiteGreyDefineUpdate(newColor)
    colorSelect_highlight.color = whiteGreyDefineUpdate(newColor)
    #
    D4_highlight.background_color = newColor
    D6_highlight.background_color = newColor
    D8_highlight.background_color = newColor
    D10_highlight.background_color = newColor
    D12_highlight.background_color = newColor
    D20_highlight.background_color = newColor
    D100_highlight.background_color = newColor
    perc_highlight.background_color = newColor
    colorSelect_highlight.background_color = newColor
    #
    header.color = newColor
    roll_header.color = newColor
    rolledHeader.color = newColor
    colorSelectHeader.color = newColor
#

If you keep the labels inside a dictionary, then this function would just look like this:

label_names = ["D4", "D6", "D8", "D10", "D12", "D20", "D100", "perc", "colorSelect"] # better make it global and use everywhere.

def colorUpdate(newColor):
    for name in label_names:
        handler.labels[name + '_highlight'].color = whiteGreyDefineUpdate(newColor)
        handler.labels[name + '_highlight'].background_color = newColor
    header.color = newColor
    roll_header.color = newColor
    rolledHeader.color = newColor
    colorSelectHeader.color = newColor
#

Questions?

tulip needle
#

Not at the moment.

whole fjord
#

Not sure if a good thing or a bad thing.