簡體   English   中英

Kivy-標簽未在用戶界面上更新

[英]Kivy - Label not updating on UI

當我單擊左側的按鈕時,應使用我單擊的按鈕的文本來更改標簽“ 2015年十大熱門”。 我可以看到變量正在更改文本,但標簽未更改文本。

這是我的工作:當我單擊左側的按鈕時,中間的標題應更改: 屏幕截圖

蟒蛇

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

基維

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'

當我打印root.title它正在更改其文本,但標簽並未更改應有的標簽,因為它是從該變量獲取文本的。

但是,當我將change_title()方法放入OtherVideos __init__中時,標簽正在更改:

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

這已經花了我幾天的時間,有人可以幫忙嗎?

您的代碼有幾個問題:

  1. 每個小部件都從EventDispatcher繼承,因此第二次從它繼承是毫無意義的。
  2. 這個root = MainApp不是對MainApp (對象)的引用,而是對其類的引用。
  3. 您無需指定StringProperty即可實現目標。

最簡單的方法是在功能change_Title添加要更改的標簽的引用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM