Set value to variable depending on input, StringProperty kivy

Related searches

I have an issue with Kivy StringProperty variable when i try to update the value based on inputs.

to make it a bit simple so I can understand too (I'm a bit new to this).

TextTime1 = StringProperty()
TextTime2 = StringProperty()
x = 2

TextTime+x = "TEST"

How can I get that to work?

I have tried

TextTime1 = StringProperty()
TextTime2 = StringProperty()
TextTimes = [TextTime1, TextTime2]
x = 2

TextTimes[x-1] = "TEST"

Does not work. Only thing that works is:

TextTime1 = StringProperty()
TextTime2 = StringProperty()

TextTime2 = "TEST"

How can I get the more "dynamic" way to work?

To my understanding you are trying to replace the last value in a list?

You could probably make it work like this

TextTime1 = StringProperty()
TextTime2 = StringProperty()

TextTimes = [TextTime1, TextTime2]

TextTimes.pop()
TextTimes.append("TEST")

Properties — Kivy 1.11.1 documentation, Value Checking / Validation: When you assign a new value to a property, the value you can't set a list to a StringProperty because the StringProperty will check the to and change the values of specified keys based on other kivy properties. new value as input and it should return the value converted to the proper type� The default value must be a value that agrees with the Property type. For example, you can’t set a list to a StringProperty because the StringProperty will check the default value. None is a special case: you can set the default value of a Property to None, but you can’t set None to a property afterward.

I think your understanding of variables is a bit shallow. For instance, TextTimes+x="TEST" is not valid because you aren't saying TextTimes2 = "TEST", you are saying TextTimes + 2 = "TEST".

Your second method isn't entirely wrong.

TextTimes[x-1] = "TEST"
# = TextTimes[1] = "TEST"
# = TextTimes2 = "TEST" since TextTimes2 is the assigned index for that variable
# in your TextTimes list.
# However the correct list should be
TextTimes = [self.TextTimes1,self.TextTimes2] # if they are properties of an object

Example:

class MyWidget(Widget):
    TextTimes1 = StringProperty()
    TextTimes2 = StringProperty()
    #TextTimes = [self.TextTimes1,self.TextTimes2]
    def __init__(self,**kwargs):
        super(MyWidget,self).__init__(**kwargs)

        self.TextTimes = [self.TextTimes1,self.TextTimes2]


    def some_function(self):
        x = 2
        self.TextTimes[x-1]="TEST" #This should change the value for TextTimes2 to "TEST"

Properties — Kivy 1.11.0 documentation, When you assign a new value to a property, the value is checked against For example, you can't set a list to a StringProperty because the StringProperty will check A ListProperty that allows you to work with a variable amount of list items and to to and change the values of specified keys based on other kivy properties. The value can be a family name (string) available in the font context (for example a system font in a system:// context, or a custom font file added using kivy.core.text.FontContextManager). If set to None, font selection is controlled by the font_name setting.

Only way I can see this work with my limited knowlage about this would be.

if x == 2:
    TextTime2 = "TEST"

That just feels wrong and alot of code if I have > 50 of them...

Text Input — Kivy 1.11.1 documentation, def on_text(instance, value): print('The widget', instance, 'have:', value) textinput = TextInput() You can set the focus to a Textinput, meaning that the input box will be highlighted and background_disabled_normal is a StringProperty and defaults to Depending on your text provider, the font file may be ignored. Set Cell Value. To set a Cell Value, use the Value property of the Range or Cells object. Range.Value & Cells.Value. There are two ways to reference cell(s) in VBA: Range Object – Range(“A2”).Value; Cells Object – Cells(2,1).Value; The Range object allows you to reference a cell using the standard “A1” notation. This will set the

Kivy Language — Kivy 1.11.1 documentation, The value of a property must be given on a single line (for now at least). “world” , a new StringProperty will be instantiated, with the default value “world” . In our example, we'll create a Toolbar, based on a BoxLayout, and put in a few Image These can be the variable part of the template that we can put into a context. The value can be a family name (string) available in the font context (for example a system font in a system:// context, or a custom font file added using kivy.core.text.FontContextManager). If set to None, font selection is controlled by the font_name setting.

Data driven variables with kivy properties, 3) I would never use global variables if they can be avoided. In this case, if all you need is access to the text content of the TextInput, I would bind it to a StringProperty attached to a widget you can access or the App object itself (which can be accessed anywhere in .kv as app.<property>.

As outlined in the illustration above, Kivy has a main loop. This loop is running during all of the application’s lifetime and only quits when exiting the application. Inside the loop, at every iteration, events are generated from user input, hardware sensors or a couple of other sources, and frames are rendered to the display.

Comments
  • Don’t you need to call ‘set()’ to set a new value? Perhaps you need: TextTimes[x-1].set( "TEST")
  • Nop, even that did now work. Tried a for x in TextTimes: x = "TEST", that set the list item to TEST i beleve. And if I did x.set("TEST") it just didnt do anything.
  • No, I want to add value to the variables in the list. Example: TextTime1 = "TEST".
  • Then why not change the value of the variable. Doesn't matter if it's part of a list or not
  • Yes, thats what I want to do. But if I have 50 of these TextTimes, connected to differend inputs. I need some sort of code to handle that. I cant do: if x == 2: TextTime2 = "TEST"...
  • You are telling that list doesn't work. What exactly doesn't work? Maybe you forgot to connect your inputs with the variables that are in the list instead of first ones?
  • Why don't you try using a dictionary? TextTimes = {"TextTimes1": "TEST1", "TextTimes2": "TEST2"}
  • Ok I will try it again.
  • Nop, cant get it to work like that. Only thing that works is if I set the string to the variable directly e.g. self.TextTime2 = "TEST"
  • You're right. I see the problem in my logic is because when i do self.texttimes[i] im just referencing the value of the property.
  • I'd be interested to know what you'd need something like this for. I've never found that I've had to use it for all my desktop developing so far.