Text from [String] to a Label.Text Isn't Working the First Time

Label text is being changed multiple times in function, but isn't updated on screen

You need to understand the way the event loop is used to update the UI, and how your code plugs into the loop.

When your code gets the control, screen rendering stops to let your code make any updates that it needs to make. Once your code returns, Cocoa analyzes the changes made inside your method, and updates the visuals accordingly.

This provides the key to solving your problem: Cocoa cannot see any changes until your method is over, including the for loop. That is why you see only the last update, while the intermediate updates go unnoticed.

In order to fix this problem you need to re-think the approach: rather than using a loop, make a method that keeps its state in a member variable. Call that method from the event handler, set the state for the first display, display the first item, set the timer to wake you up in wordsPerMinVal, and return. This would let the event loop render your changes. When the timer calls you back, change the visuals, set the timer again as needed, and return again. Continue until you are done going through the updates.

Custom Label does not show the Text string

I agree with WoLfulus and Andreas Zoltan and would add a symmetrical functionality to Text if there exists a unambiguous reverse transformation:

public string Value
{
get { return value; }
set
{
if (this.value != value) {
this.value = value;
this.Text = Transform(value);
}
}
}

public override string Text
{
get { return base.Text; }
set
{
if (base.Text != value) {
base.Text = value;
this.value = TransformBack(value);
}
}
}

Note the if checks in order to avoid an endless recursion.


EDIT:

Assigning your label to lbAttributeType is not enough. You must remove the old label from the Controls collection before the assignment and re-add it after the assignment.

this.Controls.Remove(lbAttributeType);  // Remove old label
this.lbAttributeType = new LabelBean();
this.Controls.Add(lbAttributeType); // Add new label

Your form was still displaying the old label! Why did I not see it earlier?

Why does the same code blank a label but successfully change the text property of a button in C#?

Not sure I can answer WHY this is happening, but if the data is coming in so fast, is there any use in updating the UI for every update? Who can read it anyway? How about buffering it, like putting the lastest update in some kind of data structure, and when some timer ticks, update the UI with the latest value in that data structure.

How to bind the value of a NumericProperty to the Text of a Label?

I have found a way to do it (Keep a label.text updated with the value of a Float), by using an "on_NumericProperty" function.

But I would appreciate any advice, if this is good or a bad design - or any suggested alternatives.

class MyLabel(Label):
exampleStringProperty = StringProperty("no data yet")
exampleNumericProperty = NumericProperty(0)

def on_exampleNumericProperty(self, *args):
self.exampleStringProperty = str(self.exampleNumericProperty)

def on_kv_post(self, base_widget):
Clock.schedule_interval(lambda dt : self.runlater(), 3)

def runlater(self):
self.exampleNumericProperty = 42

class TestApp(App):
def build(self): return MyLabel()


Related Topics



Leave a reply



Submit