Read from file and display in labels

845 views
Skip to first unread message

M Burroughs

unread,
Aug 4, 2016, 10:42:38 PM8/4/16
to Python - Kivy Users Group
Hello,

I am working on my first Kivy project and its time to get some help.

I am trying to read in the names from a text file into the check box labels when the app starts.
The app runs, but I can not get the label text to change.

This is what I have so far...
https://gist.github.com/burroughs-arizona/da0a9a00a7a412909a36f3b72efc3094

Thank you for your help!

Bill Janssen

unread,
Aug 5, 2016, 11:23:33 AM8/5/16
to Kivy users support
You've got a lot of kv there.  I'd probably start more like this:

from kivy.app import App
from kivy.clock import Clock
from kivy.logger import Logger
from kivy.uix.gridlayout import GridLayout
from kivy.uix.checkbox import CheckBox
from kivy.uix.label import Label


class LeftAlignedLabel(Label):

   
def __init__(self, *args, **kwargs):
       
super(LeftAlignedLabel, self).__init__(*args, **kwargs)
       
self.bind(size=self.setter("text_size"))
                                   

class CalloutList(App):

   
def build(self):

       
return GridLayout(cols=2)

   
def on_start(self):
        firefighter_file
= open("names.txt")
       
Clock.schedule_once(lambda dt: self.read_name(firefighter_file))

   
def state_change(self, name, v, *args):
       
Logger.info("CalloutList: %s changed to %s", name, v)

   
def read_name(self, firefighter_file, *args):
        name
= firefighter_file.readline()
       
if name is not None:    # check for EOF
            name
= name.strip()
           
if name:            # don't use blank lines
               
self.root.add_widget(CheckBox(
                    size_hint
=(None, None), width=50, height=40,
                    on_press
=lambda w: self.state_change(name, w.state == 'down')))
               
self.root.add_widget(LeftAlignedLabel(
                    text
=name, size_hint_y=None, height=40, valign='middle'))
           
Clock.schedule_once(lambda dt: self.read_name(firefighter_file))


if __name__ == "__main__":
   
CalloutList().run()

It's annoying that I have to keep defining LeftAlignedLabel; you'd think kivy could understand halign='left' a bit better.

Bill
Message has been deleted

ZenCODE

unread,
Aug 5, 2016, 12:40:28 PM8/5/16
to Kivy users support
@Bill Janssen.

What do you mean 'Keep defining'? It's defined once. Then subclassed. I am curious as to how you think this could be done better? I've been around long enough to know you have skills :-), so how could this be done better? It seems like a very natural "sub-class" pattern....

M Burroughs

unread,
Aug 5, 2016, 12:54:45 PM8/5/16
to Python - Kivy Users Group
Bill, thank you for taking the time to review the app!  I look forward to learning from your advice!!
I'm anxious to review the clock object and its use here.

mike b.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ZenCODE

unread,
Aug 5, 2016, 9:44:27 PM8/5/16
to Kivy users support
@M Burroughs

Hi. Also note that in the class CallOutForm, you replace the ffLABEL1 StringProperty immediately with an assignment to the class definition. The StringProperty needs to be assigned to the class definition, and then you give it values when you instantiate each instance. e.g.


class CallOutForm(GridLayout):
    ffLABEL1
= StringProperty('')  # Attach to the class definition
   
def __init__(self, **kwargs):
       
# Here is where it's instantiated
       
super(CallOutForm, self).__init__(**kwargs)  # Pass instantiation arguments to the class you're inheriting from
       
self.ffLABEL1 = "Bob"  # Now set the value
       



M Burroughs

unread,
Aug 6, 2016, 1:43:36 PM8/6/16
to Python - Kivy Users Group
Thank you for your help!!

Reply all
Reply to author
Forward
0 new messages