如何在猕猴桃中更改星形图标的颜色?

时间:2020-10-14 10:52:52

标签: python kivy

我正在使用Kivy和Kivymd。我有点发呆。 我有一个应用。

  1. 用户将主题添加到收藏夹。
  2. 用户转到“收藏夹”屏幕。
  3. 用户从收藏夹中删除主题。
  4. 用户返回到主屏幕。在这一部分中,我有一个麻烦,因为默认情况下图标的颜色必须更改颜色,但这不会发生。我不知道在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

1 个答案:

答案 0 :(得分:0)

我这样解决了。 我相信有更好的方法可以做到,但是无论如何我都可以正常工作。 我以这种方式改变了我的主班。

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