简体   繁体   中英

Kivy - Label not updating on UI

When I click on the buttons in the left part, the label "Top 10 Plays of 2015" should be changed with the text of the button that I've clicked. I can see the variable changing text but the label is not changing text.

this is my work: when I click the buttons on left side the title in the middle should change: 屏幕截图

python

class MainApp(Screen, EventDispatcher):
    vid = StringProperty("Videos/Top 10 Plays of 2015.mp4")
    title = StringProperty("Top 10 Plays of 2015")

    def __init__(self,*args,**kwargs):
        super(MainApp,self).__init__(*args, **kwargs)
    pass

class OtherVideos(BoxLayout, EventDispatcher):
    root = MainApp
    def __init__(self, *args, **kwargs):
        super(OtherVideos,self).__init__(*args, **kwargs)
        self.loadVideos()

    def loadVideos(self):
        con = MongoClient()
        db = con.nba
        vids = db.videos.find()

        vidnum = 1
        for filename in vids:
            myid = "vid" + str(vidnum)
            getfilename = filename['filename']

            button = Button(id=myid,
                          text=getfilename,
                          color=[0,0.7,1],
                          bold=1)

            button.bind(on_release=lambda x:(self.change_Title(getfilename), self.change_Vid(getfilename)))
            self.add_widget(button)
            vidnum += 1

    def change_Title(self, title):
        self.root.title = title

    def change_Vid(self, myfilename):
        con = MongoClient()
        db = con.nba
        vids = db.videos.find()

        for filename in vids:
            file = os.path.basename(filename['video_path'])
            if myfilename == filename['filename']:
                self.root.vid = filename['video_path']
                break
    pass

kivy

BoxLayout:
            orientation: 'vertical'
            Label:
                id: lblTitle
                text: root.title
                size_hint_y: None
                height: 40
                font_size: 25
                bold: 1
                canvas.before:
                    Color:
                        rgba: 1, 0, 0, 0.7
                    Rectangle:
                        pos: self.pos
                        size: self.size
            VideoPlayer:
                id: activeVid
                source: root.vid
                state: 'play'

when I'm printing root.title it's changing its text but the label is not changing which it should because it's getting its text from that variable.

But when I'm putting the change_title() method in __init__ of OtherVideos like this, the label is changing:

class OtherVideos(BoxLayout, EventDispatcher):
    root = MainApp
    def __init__(self, *args, **kwargs):
        super(OtherVideos,self).__init__(*args, **kwargs)
        self.change_Title("my title")

This is already taking me days of work, anyone can help?

There are several problems with your code:

  1. Every widget inherits from EventDispatcher , so inheriting from it the second time is pointless.
  2. This root = MainApp is not a reference to the MainApp (object), but to its class.
  3. You don't need to specify a StringProperty to achieve your goal.

The easiest way to do this is to add a reference of the label you want to change in the function change_Title :

def change_Title(self, title):
    # self.root.title = title
    self.ids.my_label_to_change.text = title

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM