如何更改kivy中星星图标的颜色?

2024-10-02 14:25:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我用的是Kivy和Kivymd。我有点难受。 我有一个应用程序

  1. 用户将主题添加到收藏夹
  2. 用户进入收藏夹屏幕
  3. 用户从收藏夹中删除主题
  4. 用户回到主屏幕。在这一部分中,我有一个truble,因为图标的颜色在默认情况下必须更改颜色,但它并没有发生。我不知道怎么走。在TopicCard/init中,我的状态正常,但图标的颜色没有改变
import kivy
from kivymd.app import MDApp
from kivy.lang import Builder
kivy.require('1.11.1')
from kivy.clock import Clock
from kivy.properties import NumericProperty, StringProperty, BooleanProperty
from kivy.utils import rgba
from kivymd.uix.card import MDCard
from kivymd.uix.button import MDIconButton
from kivy.uix.screenmanager import Screen, ScreenManager

data = [
    {'id': 1, 'topic': 'Article 1', 'favorite': False}
]

class WindowManager(ScreenManager):
    pass

class TopicCard(MDCard):
    id = NumericProperty()
    topic = StringProperty()
    favorite_topic = BooleanProperty()

    def __init__(self, **kwargs):
        super(TopicCard, self).__init__(**kwargs)

        if self.favorite_topic == True:
            self.iconButton = MDIconButton(
            id='stars',
            icon='star',
            theme_text_color='Custom',
            text_color=rgba('#E91E63')
        )
            self.iconButton.text_color = rgba('#E91E63')
            print('it is true')

        elif self.favorite_topic == False:
            self.iconButton = MDIconButton(
            id='stars',
            icon='star',
            theme_text_color='Custom',
            text_color=rgba('#000000')
        )
            self.iconButton.text_color = rgba('#000000')
            print('it is false')

        self.add_widget(self.iconButton)
        self.iconButton.bind(on_release=lambda x:self.favorite(self.id, self.topic))
#
    def favorite(self, id, topic):
        if self.favorite_topic == True:
            self.iconButton.text_color = rgba('#000000')
            self.favorite_topic = False
            for d in data:
                if d['id'] == id:
                    d['favorite'] = False
        else:
            self.iconButton.text_color = rgba('#E91E63')
            self.favorite_topic = True
            for d in data:
                if d['id'] == id:
                    d['favorite'] = True

class FavoriteScreen(Screen):
    def on_enter(self, *args):
        for x in data:
            card = TopicCard(id=x.get('id'), topic=x.get('topic'), favorite_topic=x.get('favorite'))
            self.ids.box.add_widget(card)

    def on_leave(self, *args):
        self.ids.box.clear_widgets()
        Main()

class Main(Screen):
    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)
        Clock.schedule_once(self.create_cards)

    def create_cards(self, i):
        print(data, ' < --- Create_Cards')
        for x in data:
            card = TopicCard(id=x.get('id'), topic=x.get('topic'), favorite_topic=x.get('favorite'))
            self.ids.box.add_widget(card)

class Examp(MDApp):
    def build(self):
        self.root = Builder.load_file('demo.kv')
        self.window_manager = WindowManager()
        return self.window_manager

if __name__ == '__main__':
    Examp().run()

这是我的demo.kv文件

<WindowManager>
    Main:
        id: main
        name: 'main'
    FavoriteScreen:
        id: favorite
        name: 'favorite'

<FavoriteScreen>
    BoxLayout:
        orientation: 'vertical'
        MDToolbarBack:
        ScrollView:
            BoxLayout:
                spacing: 50
                size_hint_y: None
                orientation: 'vertical'
                height: self.minimum_height
                MDList:
                    spacing: 15
                    id: box

<MDToolbarBack@MDToolbar>:
    size_hint_y: None
    height: self.theme_cls.standard_increment
    padding: '5dp'
    spacing: '12dp'

    MDIconButton:
        icon: 'arrow-left'
        pos_hint: {'center_y': .5}
        theme_text_color: 'Custom'
        text_color: 1,1,1,1
        on_press: app.window_manager.current = 'main'

    MDLabel:
        text: 'App'
        theme_text_color: 'Custom'
        text_color: 1,1,1,1

<TopicCard>
    size_hint: 1, None
    padding: 30
    MDLabel:
        text: root.topic

<Main>
    BoxLayout:
        orientation: 'vertical'
        MDToolbar:
            title: 'App'
            MDFlatButton:
                theme_text_color: 'Custom'
                text_color: 1,1,1,1
                font_size: '18dp'
                text: 'Go to Favorite'
                pos_hint: {'center_y': .5}
                on_press: app.window_manager.current = 'favorite'

        ScrollView:
            BoxLayout:
                spacing: 50
                size_hint_y: None
                orientation: 'vertical'
                height: self.minimum_height
                MDList:
                    spacing: 15
                    id: box


Tags: textfromimportselfiddatatopicdef
1条回答
网友
1楼 · 发布于 2024-10-02 14:25:20

我是这样决定的。 我确信有更好的方法做这件事,但不管怎样,它对我来说很好。 我就这样换了主课

class Main(Screen):

    get_ids = None

    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)
        Clock.schedule_once(self.create_cards)

    def on_pre_enter(self, *args):
        if self.get_ids != None:
            for x in data:
                card = TopicCard(id=x.get('id'), topic=x.get('topic'), favorite_topic=x.get('favorite'))
                self.ids.box.add_widget(card)
        print('I am from on_pre_enter')

    def create_cards(self, i):
        self.get_ids = self.ids.box
        for x in data:
            card = TopicCard(id=x.get('id'), topic=x.get('topic'), favorite_topic=x.get('favorite'))
            self.ids.box.add_widget(card)

    def on_leave(self, *args):
        self.ids.box.clear_widgets()

class Examp(MDApp):
    def build(self):
        self.root = Builder.load_file('demo.kv')
        self.window_manager = WindowManager()
        return self.window_manager

if __name__ == '__main__':
    Examp().run()

相关问题 更多 >