#class LabelHandler
1 messages · Page 1 of 1 (latest)
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.
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
I'm not sure I understand what you're trying to say. Are you suggesting a static class?
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.
There's a lot of ways of making the code more scalable and readable, yeah.
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.
then use a dictionary self.labels = {} and set them like so:
self.labels['my_label'] = label
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
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.
Wouldn't you have add label also take in the label name? So you can add multiple?
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.
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.
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.
But how does the class know that this:
handler.addLabel('Some text and stuff')
is the "my_label"?
Is each label static?
What do you mean by static?
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
]
Static as in doesn't change
Please clarify your question
For that it would be: handler.addLabel('label_1', 'label text')
I think I understand, you also need to pass the name that you'll be using for the label:
def addLabel(self, name, text, x, y, color):
label = Label(text, x, y, color) # whatever the logic you are using to create the label
self.labels[name] = label # add the label
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.
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.
As of right now it's handleing those labels.
Stepping away, feel free to reach out directly if I can help later.
Okay
More loops for what specifcally? Like, I don't get it at all.
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..
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.
I understand what you mean. I just.. I dunno. I'm SO confused.
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.
Well, I think the problem is that you don't fully understand what you write.
Maybe that's a good way to put it.
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.
I get that
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?
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?
Not at the moment.
Not sure if a good thing or a bad thing.