【问题标题】:Kivy - doesn't refresh/change data when the data has been modified?Kivy - 修改数据后不刷新/更改数据?
【发布时间】:2016-02-19 06:48:27
【问题描述】:

所以我正在创建一个类似于 Youtube 但不是在线的视频播放器。所以我的活动视频布局由标题和视频组成,另一个布局是相关视频,目前由其他视频的标题组成。

class MainApp(Screen):
    vid = "Top 10 Plays of 2015"
    title = "Top 10 Plays of 2015"

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

class OtherVideos(BoxLayout): 
    def __init__(self, **kwargs):
        super(OtherVideos,self).__init__(**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)
            label = Label(id=myid,
                          markup=True,
                          text="[ref=act]" + filename['filename'] + "[/ref]",
                          color=[0,0.7,1],
                          bold=1)

            label.bind(on_ref_press=lambda inst, val:self.change_Title(filename['filename']))

            self.add_widget(label)

            vidnum += 1

    def change_Title(self, next_title):
        MainApp.title = next_title
    pass

在我的 kivy 代码中:

 Label:
      id: lblTitle
      text: root.title

所以我要做的是在 MainApp 类中编辑标题变量,因此我需要单击标签(这是其他视频布局中的标题)并且应该更改标题变量。标签是根据数据库中的数据动态创建的。

我可以通过 (print (MainApp.title)) 看到 shell 上的 title 变量发生变化,但它在我的布局屏幕上没有变化。它仍然是“2015 年十大戏剧”。

TLDR;变量 'title' 在 shell 上发生变化,但在布局屏幕上没有变化。

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    你需要使用 StringProperty。

    示例:

    class MainApp(Screen):
        vid = StringProperty("Top 10 Plays of 2015")
        title = StringProperty("Top 10 Plays of 2015") #yay!!!
    
        def __init__(self,**kwargs):
            super(MainApp,self).__init__(**kwargs)
    

    【讨论】:

    • 它说:未定义 StringProperty
    • @KPA 你是from kivy.properties import StringProperty 吗?请参考programming guide
    猜你喜欢
    • 1970-01-01
    • 2021-10-15
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多