👍 app.py: Implement Kivy app with word pair generation and liking functionality 📝 requirements.txt: Add dependencies required for the Kivy app
162 lines
4.0 KiB
Python
162 lines
4.0 KiB
Python
from kivy.lang import Builder
|
|
from kivy.properties import StringProperty, ListProperty
|
|
from kivymd.app import MDApp
|
|
from kivymd.uix.label import MDLabel
|
|
from kivymd.uix.navigationbar import MDNavigationBar, MDNavigationItem
|
|
from kivymd.uix.navigationbar.navigationbar import MDBoxLayout
|
|
from kivymd.uix.screen import MDScreen
|
|
from wonderwords import RandomWord
|
|
from kivymd.uix.card import MDCard
|
|
from kivy.event import EventDispatcher
|
|
from kivy.uix.scrollview import ScrollView
|
|
|
|
KV = '''
|
|
<BaseMDNavigationItem>
|
|
|
|
MDNavigationItemIcon:
|
|
icon: root.icon
|
|
|
|
MDNavigationItemLabel:
|
|
text: root.text
|
|
|
|
<MainScreen>
|
|
name: "main_screen"
|
|
MDBoxLayout:
|
|
orientation: "vertical"
|
|
pos_hint: {"center_x": .5, "center_y": .5}
|
|
|
|
MDCard:
|
|
padding: "4dp"
|
|
size_hint: None, None
|
|
size: "240dp", "100dp"
|
|
|
|
MDRelativeLayout:
|
|
|
|
MDLabel:
|
|
text : root.currentWordPair
|
|
adaptive_size: True
|
|
color: "black"
|
|
pos: "12dp", "12dp"
|
|
bold: True
|
|
|
|
MDBoxLayout:
|
|
|
|
MDButton:
|
|
style: "elevated"
|
|
pos_hint: {"center_x": .5, "center_y": .5}
|
|
on_press: root.new_word_pair(*args)
|
|
|
|
MDButtonIcon:
|
|
icon: "plus"
|
|
|
|
MDButtonText:
|
|
text: "Suivant"
|
|
|
|
MDButton:
|
|
style: "elevated"
|
|
pos_hint: {"center_x": .5, "center_y": .5}
|
|
on_press: root.like_word_pair(*args)
|
|
|
|
MDButtonIcon:
|
|
icon: "plus"
|
|
|
|
MDButtonText:
|
|
text: "Like"
|
|
|
|
<FavoriteScreen>
|
|
ScrollView:
|
|
MDBoxLayout:
|
|
id: favorite_layout
|
|
orientation: "vertical"
|
|
|
|
MDBoxLayout:
|
|
orientation: "vertical"
|
|
md_bg_color: self.theme_cls.backgroundColor
|
|
|
|
MDScreenManager:
|
|
id: screen_manager
|
|
|
|
MainScreen:
|
|
name: "Word Pair"
|
|
image_size: "1024"
|
|
|
|
FavoriteScreen:
|
|
name: "Favorites"
|
|
image_size: "800"
|
|
|
|
MDNavigationBar:
|
|
on_switch_tabs: app.on_switch_tabs(*args)
|
|
|
|
BaseMDNavigationItem
|
|
icon: "gmail"
|
|
text: "Word Pair"
|
|
active: True
|
|
|
|
BaseMDNavigationItem
|
|
icon: "twitter"
|
|
text: "Favorites"
|
|
'''
|
|
|
|
r = RandomWord()
|
|
|
|
class WordList():
|
|
words = []
|
|
|
|
class BaseMDNavigationItem(MDNavigationItem):
|
|
icon = StringProperty()
|
|
text = StringProperty()
|
|
|
|
class LikedEventDispatcher(EventDispatcher):
|
|
def __init__(self, **kwargs):
|
|
self.register_event_type('on_like')
|
|
super(LikedEventDispatcher, self).__init__(**kwargs)
|
|
|
|
def on_like(self, *args):
|
|
print("a word pair has been liked", args)
|
|
|
|
class MainScreen(MDScreen):
|
|
currentWordPair = StringProperty("TidalWave")
|
|
|
|
def new_word_pair(self,instance):
|
|
print(MDApp.get_running_app().liked)
|
|
self.currentWordPair = r.word().capitalize() + r.word().capitalize()
|
|
|
|
def like_word_pair(self,instance):
|
|
MDApp.get_running_app().liked.append(self.currentWordPair)
|
|
layout = self.manager.get_screen("Favorites").ids.favorite_layout
|
|
layout.clear_widgets()
|
|
for word in MDApp.get_running_app().liked:
|
|
print(word)
|
|
card = MDCard()
|
|
label = MDLabel(
|
|
text=word,
|
|
color="black",
|
|
)
|
|
card.add_widget(label)
|
|
layout.add_widget(card)
|
|
|
|
class FavoriteScreen(MDScreen):
|
|
image_size = StringProperty()
|
|
|
|
class Example(MDApp):
|
|
|
|
liked = ListProperty([])
|
|
|
|
def add_to_liked(self):
|
|
self.liked.append()
|
|
pass
|
|
|
|
def on_switch_tabs(
|
|
self,
|
|
bar: MDNavigationBar,
|
|
item: MDNavigationItem,
|
|
item_icon: str,
|
|
item_text: str,
|
|
):
|
|
self.root.ids.screen_manager.current = item_text
|
|
|
|
def build(self):
|
|
return Builder.load_string(KV)
|
|
|
|
Example().run()
|