【问题标题】:Python Kivy recycle view prints extra buttonPython Kivy 回收视图打印额外的按钮
【发布时间】:2018-06-14 16:01:04
【问题描述】:

我正在尝试在我的 kivy 程序中实现一个回收视图小部件。它工作正常,响应 on press 事件等,但是当它绘制时,它添加了一个不适合其他格式的额外行。这是我的代码的相关部分,有很多所以我只包括了这个,但如果需要我会发布更多。任何帮助表示赞赏。我喜欢 kivy,但这是我用过的最奇怪的小部件。

输出

py 文件

class MessageBox(Popup):

    def popup_dismiss(self):
        self.dismiss()

class SelectableButton(RecycleDataViewBehavior, Button):
    index = None

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

    def on_press(self):
        self.parent.selected_value = 'Selected: {}'.format(self.parent.btn_info[int(self.id)])

    def on_release(self):
        MessageBox().open()

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
    selected_value = StringProperty('')
    btn_info = ListProperty(["ButtonText" for x in range(0,2)])

class MainScreen(RecycleView):
    rv_layout = ObjectProperty(None)
    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.data = [{'text': "Button" + str(x), 'id': str(x)} for x in range(0,2)]

kv文件

<MessageBox>:
    title: 'Popup Message Box'
    size_hint: None, None
    size: 400, 400

    BoxLayout:
        orientation: 'vertical'
        #Label:
            #text: app.root.rv_layout.selected_value
        Button:
            size_hint: 1, 0.2
            text: 'OK'
            on_press:
                root.dismiss()

<SelectableButton>:
    orientation: 'horizontal'
    Button:
        size_hint_x: .10
        text: '+'
        #font_size: 50
        texture_size: self.width, self.height
        size: self.texture_size
    Button:
        size_hint_x: .90
        text: root.text
        #font_size: 50
        texture_size: self.width, self.height
        size: self.texture_size

<MainScreen>:
    pos_hint: {'x': 0, 'y': .11}
    size_hint: 1, .70
    viewclass: 'SelectableButton'
    SelectableRecycleBoxLayout:
        id: layout
        default_size_hint: 1, 1
        #size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'   

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    问题 - 额外的按钮

    Python文件中,SelectableButtonButton的一个类> 小部件,但在 kv 文件中,SelectableButton 的一个类带有两个按钮的 BoxLayout 小部件。按钮小部件没有属性,orientation,但 BoxLayout 有这个属性。由于小部件继承不匹配,Kivy 使用了 Python 脚本中的定义。

    至于可视化,我在 kv 文件中定义的两个 Button 中添加了背景颜色(红色和蓝色)。在下面的打印屏幕中,额外的按钮来自 kv 文件。

    py 文件

    class SelectableButton(RecycleDataViewBehavior, Button):
    

    kv 文件

    <SelectableButton>:
        orientation: 'horizontal'
        Button:
            ...
        Button:
            ...
    

    解决方案

    更改 Python 文件或 kv 文件。在示例中,更改了 kv 文件。

    kv 文件

    <SelectableButton>:
        size_hint_x: .90
        size: self.texture_size
    

    输出 - 原始 Kivy 应用程序的可视化

    输出 - 修复 Kivy 应用程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多