Home > Blockchain >  How to integrate elements from KV to Python?
How to integrate elements from KV to Python?

Time:07-13

I'm trying to make a simple application that uses buttons to cycle through different strings with a label, but how would one get the strings from the .py file to the KV string?

For example, I want it to say '[x_string]' in the middle of the screen. When the user presses a button (arrow facing right), it goes to '[y_string]' instead. When the user presses the other button (arrow facing left), it goes back to '[x_string]', etc.

Here is the code I have so far.

from PyDictionary import PyDictionary
from kivy.metrics import dp
from kivy.properties import StringProperty
from kivymd.uix.list import OneLineIconListItem
from kivymd.uix.menu import MDDropdownMenu
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.toolbar import MDToolbar
from kivy.core.window import Window
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton
from kivymd.uix.swiper import MDSwiper
from kivymd.uix.label import MDLabel
from pathlib import Path


KV = '''
ScreenManager:
    Screen1:

<Screen1>:
    name: 'screen'

    MDScreen:
        name: 'screen'

        MDToolbar:
            title: 'Test App'
            elevation: 20
            pos_hint: {'top': 1}
            md_bg_color: (1, 1, 1, 1)
            specific_text_color: (76/255, 76/255, 77/255, 1)
            right_action_items: [["information"],["home"]]
        
        FloatLayout:
            
            # Left Arrow
            Button:
                background_normal: 'buttonsNormal/chevron_arrow_left.png'
                background_down: 'buttonsDown/chevron_arrow_left_down.png'
                size_hint: (0.06, 0.16)
                pos_hint: {'center_x': 0.06, 'center_y': 0.445}
                border: [0, 0, 0, 0]
            
            # Right Arrow
            Button:
                background_normal: 'buttonsNormal/chevron_arrow_right.png'
                background_down: 'buttonsDown/chevron_arrow_right_down.png'
                size_hint: (0.06, 0.16)
                pos_hint: {'center_x': 0.94, 'center_y': 0.445}
                border: [0, 0, 0, 0]

'''

Window.size = (1280, 720)

class Screen1(Screen):
    def build(self):
        MDLabel(
            text = 'Raise your arm.',
            font_size = dp(56),
            halign = 'center'
        )

class MainApp(MDApp):
    dialog = None

    def build(self):
        sm = ScreenManager()
        sm.add_widget(Screen1(name = 'follow_screen'))
        return Builder.load_string(KV)

MainApp().run()

I am leaving out about 800 lines of completely unrelated code, so that's why there are so many imported modules as the top.

Thank you!

CodePudding user response:

You can add a Label to the Screen1 kv:

        Label:
            text: root.text
            size_hint: 0.25, 0.25
            color: 0,0,0,1
            size: self.texture_size
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}

The text for this Label is set to root.text, which should be a StringProperty in the Screen1 class:

class Screen1(Screen):
    text = StringProperty('x_string')

Then, you can adjust the value of the StringProperty Like this:

        # Left Arrow
        Button:
            background_normal: 'buttonsNormal/chevron_arrow_left.png'
            background_down: 'buttonsDown/chevron_arrow_left_down.png'
            size_hint: (0.06, 0.16)
            pos_hint: {'center_x': 0.06, 'center_y': 0.445}
            border: [0, 0, 0, 0]
            on_press:
                root.text = 'x_string'

        # Right Arrow
        Button:
            background_normal: 'buttonsNormal/chevron_arrow_right.png'
            background_down: 'buttonsDown/chevron_arrow_right_down.png'
            size_hint: (0.06, 0.16)
            pos_hint: {'center_x': 0.94, 'center_y': 0.445}
            border: [0, 0, 0, 0]
            on_press:
                root.text = 'y_string'
  • Related