【问题标题】:Kivy - Label not updating on UIKivy - 标签未在 UI 上更新
【发布时间】:2016-02-20 05:23:15
【问题描述】:

当我单击左侧的按钮时,标签“2015 年 10 大戏剧”应该会更改为我单击的按钮的文本。我可以看到变量正在更改文本,但标签没有更改文本。

这是我的工作:当我单击左侧的按钮时,中间的标题应该会改变:

蟒蛇

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")

这已经需要我几天的工作了,有人可以帮忙吗?

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    你的代码有几个问题:

    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
    

    【讨论】:

    • 感谢您的回复,但我得到的 'super' 对象没有属性 'getattr'
    • @KPA 然后你正在尝试访问当前范围内不存在的 id。
    • 我已经有它作为lblTitle,我不知道为什么它没有被检测到
    • @KPA 每个 id 都被限制在它定义的范围内。它不是全球性的。如果您想在OtherVideos 框布局中使用lblTitle 的id,则需要将其链接到那里。这是一个基本的例子:kivy.org/docs/guide/lang.html#referencing-widgets(如果太基本,那么搜索,我已经回答了这样的问题)
    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多