首页 > 解决方案 > 使用 app.root blabla 引用 .kv 文件中的属性

问题描述

下面是我从这个链接修改的代码!添加了一个屏幕小部件和一个定义的recycleview 视图类。这个概念是,当我按下recycleview 项目中的按钮时,必须出现一个带有按钮名称的弹出窗口,或者必须将数据传输到弹出窗口小部件。我已经能够在终端上打印出按钮名称,但无法在弹出小部件上打印它。挑战在于如何在 .kv 中使用 app.root.... 方法引用 'selected_value = StringProperty('')'。这花了我几天的时间试图弄清楚,因为我无法继续。请我需要帮助!

主文件

import kivy
kivy.require('1.10.1')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout  
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager,Screen,FadeTransition,SlideTransition
from kivy.properties import *
from kivy.uix.behaviors import ButtonBehavior, FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.popup import Popup
from kivy.core.window import Window
Window.size = (100 * 5, 90 * 8)

class ScreenOne(Screen):
    def go(self):
    self.manager.current = 'two'

class ScreenTwo(Screen):
    def __init__(self,**kwargs):
        super(ScreenTwo,self).__init__(**kwargs)

############## creating a popup MessageBox #####################################
class MessageBox(Popup):
    def popup_dismiss(self):
        self.dismiss()

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
    selected_value = StringProperty('') # How can I reference this property in my .kv file to appear on my Messagebox widget label
    info = ListProperty(['Button 1','Button 2','Button 3','Button 4','Button 5','Button 6','Button 7','Button 8', 'Button 9','Button 10','Button 11','Button 12','Button 13','Button 14','Button 15','Button 16','Button 17','Button 18','Button 19','Button 20'])

class ViewClass(RecycleDataViewBehavior, BoxLayout):
    id_num = StringProperty()
    button = StringProperty()
    index = None

    def refresh_view_attrs(self, rv, index, data):
        """ Catch and handle the view changes """
        self.index = index
        return super(ViewClass, self).refresh_view_attrs(rv, index, data)

    def on_press(self):
        self.parent.selected_value = '{}'.format(self.parent.info[int(self.id_num)])
        MessageBox().open()

    def on_release(self):
        pass

class RV(RecycleView):
    rv_layout = ObjectProperty(None)
    def __init__(self, **kwargs):
         super(RV, self).__init__(**kwargs)
         self.data = [{'id_num':str(x),'button': 'Button %s' % str(x)} for x in range(20)]

class MainApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(ScreenOne(name="one"))
        sm.add_widget(ScreenTwo(name="two"))
        sm.current = 'one'
        return sm

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

主文件

#: kivy 1.10.1
#: import Label kivy.uix.button.Label
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import FallOutTransition kivy.uix.screenmanager.FallOutTransition

<ScreenOne>:
    name:'verify_no'
    BoxLayout:
        orientation: 'vertical'
        padding:100,100
        spacing: 50
        GridLayout:
            cols: 1
            Label:
                text: 'press to go to screen two'
                id: ti1
            Button:
                text: 'Go to >'
                on_press: root.go()

<MessageBox>:
    title: 'Order'
    size_hint: None, None
    size: 400, 400
    BoxLayout:
        orientation: 'vertical'
        spacing: 50
        GridLayout:
            cols: 1
            Label:
                text:'app.root.parent.rv_layout.selected_value:\nproduces Attribute error'
            Label:
                text:'app.root.parent.rv_layout.selected_value:\nalso produces NoneType error'
            Label:
                text:'app.root.children.rv_layout.selected_value:\ndid not work neither'
        BoxLayout:
             orientation:'horizontal'
             Button:
                size_hint: 1, 0.2
                text: 'ok'
                on_press:
                    root.dismiss()

 <MyButton@Button>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (0.0, 0.9, 0.1, 0.3)
        Rectangle:
            pos: self.pos
            size: self.size

<ViewClass>:
    orientation:'vertical'
    BoxLayout:
        padding: 15
        Label:
            text: root.id_num
        MyButton:
            id:bt
            text: root.button
            background_color: 0,0,0,0
            on_press: root.on_press()

    Label:
        canvas.before:
            Color:
                rgba: (1,1,1,1)
            Rectangle:
                size: self.size
                pos: self.pos
        size_hint_y: None
        height: 1

<ScreenTwo>:
    name:'two'
    id: itm
    BoxLayout:
       id:b2
       padding:1,1
       spacing:1
       orientation: 'vertical'

       RV:
           rv_layout: layout
           scroll_type: ['bars', 'content']
           scroll_wheel_distance: dp(114)
           bar_width: dp(10)
           viewclass: 'ViewClass'
           SelectableRecycleBoxLayout:
               id: layout
               default_size: None, dp(56)
               default_size_hint: 1, None
               size_hint_y: None
               height: self.minimum_height
               orientation: 'vertical'

标签: pythonkivykivy-language

解决方案


我自己仍然在整个 Kivy 1.10.1 中磕磕绊绊,但我认为您正在尝试引用尚不存在的东西。同一个线程有一个更好的(恕我直言)示例。

尝试在 RecycleView ( RV ) 中创建一个函数供StringProperty()引用。

class MessageBox(Popup):
    message = StringProperty()

def message(self, message)
    p = MessageBox()
    p.message = message
    p.open()

然后在您的 KV 中在您的BoxLayout:中引用它:

 Label:
        text: root.message

推荐阅读