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

2024-09-30 16:40:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试只使用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

Tags: textfromposimportselfappsizebutton
1条回答
网友
1楼 · 发布于 2024-09-30 16:40:43

有几个问题:

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

When adding widgets, we need to specify the height manually

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

因此,考虑到所有这些,我创建了一个稍微修改的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文件,以便您可以在kv文件中创建MyDropDown。我还将height规范添加到了添加到DropDownWidgets中。MyDropDown.open()调用现在包括self(即Button

相关问题 更多 >