【问题标题】:Kivy Label not updating after a changeKivy 标签在更改后未更新
【发布时间】:2021-07-21 03:08:33
【问题描述】:

a.py

from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.app import App
from kivy.properties import StringProperty, BooleanProperty, ObjectProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from enum import Enum
from enum import auto
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
import threading

class menu(Enum):
    Espresso = (0)
    Latte = (1)
    Sahlep = (2)
    TurkKahvesi = (3)
    def __init__(self, id):
        self.id = id


class Test(Screen):
    pass

class MainWindow(Screen):
    pass

class Third(Screen):
    pass


class SecondWindow(Screen):
    pass


class WindowManager(ScreenManager):
    pass

class Tabs(TabbedPanel):
    pass

class Labs():
    pass

class CountSigns(StackLayout):
    my_text = 0
    count = 0

    def on_button_click_plus(self):
        print("Button clicked plus")
        self.count += 1
        self.my_text = str(self.count)
        print(self.my_text)

        return self.my_text
    def on_button_click_minus(self):
        print("Button clicked minus")
        self.my_text = str(self.count)

        if self.count > 0:
            self.count -= 1

        else:
            pass

    def __init__(self, **kwargs):
        value = str(self.my_text)
        super().__init__(**kwargs)
        b = Button(text='latte', size_hint=(None, None), size=(100, 100))
        self.add_widget(b)
        b.bind(on_press=lambda x: self.on_button_click_plus())


        lab = Label(text= str(self.my_text), color= (1, .5, 1, 1), size=(50, 100), size_hint=(None, None))
        self.add_widget(lab)


        btn1 = Button(text='-', size_hint=(None, None), size=(50, 100))
        btn1.bind(on_press=lambda x: self.on_button_click_minus())
        self.add_widget(btn1)




class WidgetsExample(CountSigns):
    value = CountSigns.my_text

class WidgetsExample2(GridLayout):
    def __init__(self,**kwargs):
        super(WidgetsExample2,self).__init__(**kwargs)
        self.top_grid = GridLayout() # Widget above main widget to hold all text and input boxes.
        self.cols = 2 # no.of columns


kv = Builder.load_file("b.kv")


class MyMainApp(App):
    def build(self):
        return kv


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

b.kv

WindowManager:
    MainWindow:
    Third:


<MainWindow>:
    name: "main"

    GridLayout:
        cols:2


        Button:
            text: "New Order"
            on_release:
                app.root.current = "third"
                root.manager.transition.direction = "left"


<Third>:
    name: "third"
    Tabs:
        do_default_tab: False

        TabbedPanelItem:
            text: 'Hot Drinks'
            CountSigns:

因此,当我运行此代码时,“实验室”中的 my_text 不会在 GUI 中更新。然而,打印显示 my_text 实际上发生了变化。我怎样才能使标签更新。我基本上希望按钮本身使值上升,减号按钮使值下降。当我的代码在 kv 文件中时它确实有效,但是因为我需要存储我决定需要在 python 文件中定义这些值的值。

【问题讨论】:

    标签: python kivy label


    【解决方案1】:

    您更新了您的“self.my_text”,但当您单击“-”或“+”时实际上并没有更新您的标签

    class CountSigns(StackLayout):
        my_text = 0
        count = 0
        
        lab = Label(text="", color=(1, .5, 1, 1), size=(50, 100), size_hint=(None, None))
    
        def on_button_click_plus(self):
            print("Button clicked plus")
            
            self.count += 1
            self.my_text = str(self.count)
            self.lab.text = str(self.my_text)
    
            return self.my_text
    
        def on_button_click_minus(self):
            print("Button clicked minus")
    
            self.my_text = str(self.count)
            self.lab.text = str(self.my_text)
    
            if self.count > 0:
                self.count -= 1
    
            else:
                pass
    
        def __init__(self, **kwargs):
            value = str(self.my_text)
            super().__init__(**kwargs)
            b = Button(text='latte', size_hint=(None, None), size=(100, 100))
            self.add_widget(b)
            b.bind(on_press=lambda x: self.on_button_click_plus())
    
    
            self.lab.text = str(self.my_text)
            self.add_widget(self.lab)
    
    
            btn1 = Button(text='-', size_hint=(None, None), size=(50, 100))
            btn1.bind(on_press=lambda x: self.on_button_click_minus())
            self.add_widget(btn1)
    

    您可以将 Label 定义为 Class 属性,以便在需要时访问/更新它。

    编辑: 你也可以像以前一样工作;在 .kv 文件中配置您的标签/按钮。但是,您将不得不使用 id。也许这也是保持 .py 文件“干净”的一种选择,以防您添加大量小部件。

    EDIT2(带有 .kv 文件中的小部件):

    .py

    class CountSigns(StackLayout):
        my_text = 0
        count = 0
    
        def on_button_click_plus(self):
            print("Button clicked plus")
    
            self.count += 1
            self.my_text = str(self.count)
            self.ids['label_id'].text = str(self.my_text)
    
            return self.my_text
    
        def on_button_click_minus(self):
            print("Button clicked minus")
    
            if self.count > 0:
                self.count -= 1
            else:
                pass
    
            self.my_text = str(self.count)
            self.ids['label_id'].text = str(self.my_text)
    
        def __init__(self, **kwargs):
            value = str(self.my_text)
            super().__init__(**kwargs)
    

    .kv

    WindowManager:
        MainWindow:
        Third:
    
    
    <MainWindow>:
        name: "main"
    
        GridLayout:
            cols:2
    
    
            Button:
                text: "New Order"
                on_release:
                    app.root.current = "third"
                    root.manager.transition.direction = "left"
    
    
    <CountSigns>:
        plus_sign: plus_sign
        label_id: label_id
        min_sign: min_sign
    
        Button:
            id: plus_sign
            text: 'latte'
            size_hint: (None, None)
            size: (100, 100)
            on_press:
                root.on_button_click_plus()
    
        Label:
            id: label_id
            text: str(root.my_text)
            color: (1, .5, 1, 1)
            size: (50, 100)
            size_hint: (None, None)
    
        Button:
            id: min_sign
            text: '-'
            size_hint: (None, None)
            size: (50, 100)
            on_press:
                root.on_button_click_minus()
    
    
    
    <Third>:
        name: "third"
        Tabs:
            do_default_tab: False
    
            TabbedPanelItem:
                text: 'Hot Drinks'
                CountSigns:
    

    ps:当你减少'on_button_click_minus'时,先减少,然后更新标签。现在您单击并没有任何反应,因为您在更新标签后会减少。

    【讨论】:

    • 问题是我想要两个按钮之间的标签,如果我分开它,我不知道该怎么做
    • 我在哪里可以查看 ID 的工作方式?
    • 检查最新的编辑,应该可以。只是谷歌很多关于 kivy ids 的信息。并搜索一些代码示例。您搜索的越多,您就越能掌握它。
    • 像我给你的例子一样使用'id'属性。然后在你的 Python 文件中,使用 'self.ids['id_of_your_widget'] 来访问它。我认为您不能真正在 .kv 文件本身中连接字符串等,我猜应该在 .py 中完成
    【解决方案2】:
    <CountSigns>:
        drink: drink
        plus_sign: plus_sign
        label_id: label_id
        min_sign: min_sign
    
        Label:
            id: drink
            text: 'coffee'
            size_hint: (None, None)
            size: (100, 100)
        Button:
            id: plus_sign
            text: '+'
            size_hint: (None, None)
            size: (50, 100)
            on_press:
                root.on_button_click_plus()
    
        Label:
            id: label_id
            text: str(root.my_text)
            color: (1, .5, 1, 1)
            size: (50, 100)
            size_hint: (None, None)
    
        Button:
            id: min_sign
            text: '-'
            size_hint: (None, None)
            size: (50, 100)
            on_press:
                root.on_button_click_minus()
    
    
    
    
    <StackLayoutExample>
        CountSigns:
        CountSigns:
            root.ids.drink.text: 'peach'
        CountSigns:
    

    如何在 kv 文件中使用 ids 我尝试使用 root.ids.drink.text: 'peach' 更改标签文本,但这没有用。有没有可能做到这一点?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      • 2020-11-06
      • 1970-01-01
      • 2015-06-09
      相关资源
      最近更新 更多