我用的是Kivy和Kivymd。我有点难受。 我有一个应用程序
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
我是这样决定的。 我确信有更好的方法做这件事,但不管怎样,它对我来说很好。 我就这样换了主课
相关问题 更多 >
编程相关推荐