下拉菜单无法使用Kivy语言工作

问题描述 投票:0回答:1

我试图仅使用Kivy语言制作一个简单的下拉菜单。

此程序是一个简单的图像,用户可以通过单击下拉菜单的按钮来调整其大小。程序启动时,下拉菜单的一部分显示在底部附近。除此之外,一切看起来都不错。单击后,什么都没有发生,除了下拉菜单中可见的部分(我不希望可见)消失了。

# .py file
import kivy 
from kivy.app import App 
# kivy.require('1.9.0') 

from kivy.uix.scatter import Scatter 
from kivy.uix.widget import Widget 
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button

# Creating widget class 
class SquareWidget(Widget):
    pass
# Creating Scatter Class 
class ScatterWidget(Scatter):
    do_rotation=False

# Create the layout class 
class Scatter_App(RelativeLayout):
    pass

class ScatterApp(App): 
    def build(self):
        return Scatter_App()

if __name__=='__main__': 
    ScatterApp().run()
# .kv file
# Create the scatter properties        
<SquareWidget>:
    size: self.parent.size
    canvas:
        Rectangle:
            size: self.size 
            pos: self.pos
            source: 'image.jpg'  

<Scatter_App>:
    canvas: 
        Rectangle: 
            size: self.size 
            pos: self.pos 

    ScatterWidget: 
        id: square_widget_id 
        SquareWidget:

    DropDown:
        id: cdd
        Button:
            text: 'Item 1'
        Label:
            text: 'Item 2'
        Label:
            text: 'Item 3'

    Button:
        background_normal: ''
        background_color: 1, .2, .3, .85
        text: 'Choose'
        text_size: self.size
        text_pos: self.height/2,self.width/2
        size_hint: .15,.15
        pos: (self.parent.width-self.width)/2,self.parent.height-self.height
        on_release: cdd.open
python binding kivy dropdown
1个回答
0
投票

一些问题:

  • 如果直接在DropDown中添加kv,它将像其他Scatter_App一样成为Widget的子级。然后,尝试在其上调用open()将失败,因为open()尝试执行add_widget(但是DropDown已经有一个父级)。
  • 您对DropDown的规则未指定所添加的每个Widgets的高度。从documentation

添加小部件时,我们需要手动指定高度

  • [在open()上调用DropDown时,必须包含一个参数,该参数指定Widget应该附加到的DropDown

因此,考虑到所有这些,我为您的kv文件创建了一个稍微修改的版本:

# .kv file
# Create the scatter properties     
#:import Factory kivy.factory.Factory
<SquareWidget>:
    size: self.parent.size
    canvas:
        Rectangle:
            size: self.size 
            pos: self.pos
            source: 'image.jpg'  

# add a dynamic class that extends DropDown
<MyDropDown@DropDown>:
    Button:
        text: 'Item 1'
        size_hint_y: None
        height: 40
    Label:
        text: 'Item 2'
        size_hint_y: None
        height: 40
    Label:
        text: 'Item 3'
        size_hint_y: None
        height: 40

<Scatter_App>:
    canvas: 
        Rectangle: 
            size: self.size 
            pos: self.pos 

    ScatterWidget: 
        id: square_widget_id 
        SquareWidget:

    Button:
        background_normal: ''
        background_color: 1, .2, .3, .85
        text: 'Choose'
        text_size: self.size
        text_pos: self.height/2,self.width/2
        size_hint: .15,.15
        pos: (self.parent.width-self.width)/2,self.parent.height-self.height
        on_release: Factory.MyDropDown().open(self)

我已将Factory导入添加到kv文件,以便可以在MyDropDown文件中创建kv。我也将height规范添加到了Widgets中添加的DropDown中。 MyDropDown.open()呼叫现在包括selfButton)。

© www.soinside.com 2019 - 2024. All rights reserved.