使用app.root引用.kv文件中的属性blabla

时间:2018-11-28 18:45:49

标签: python kivy kivy-language

下面是我从此link修改的代码!加上屏幕小部件和recycleview的定义的viewclass。想法是,当我在recycleview项中按下按钮时,弹出窗口必须显示其按钮名称,或者必须将数据传输到弹出窗口小部件。我已经能够在终端上打印出按钮名称,但是无法在弹出窗口小部件上打印出来。面临的挑战是如何使用.kv中的app.root ....方法引用“ selected_value = StringProperty('')”。这花了我几天时间试图弄清楚,因为我无法继续。请我需要帮助!!!

main.py

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

main.kv

#: 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'

1 个答案:

答案 0 :(得分:0)

我本人仍在绊脚整个Kivy 1.10.1,但我认为您正在尝试引用尚不存在的内容。同一线程有一个更好的(IMHO)示例。

尝试在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