【问题标题】:Dropdown menu not working using Kivy Language下拉菜单无法使用 Kivy 语言
【发布时间】:2020-02-29 02:08:04
【问题描述】:

我正在尝试仅使用 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】:

    几个问题:

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

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

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

    我已在kv 文件中添加了Factory 导入,以便您可以在kv 文件中创建MyDropDown。我还将height 规范添加到Widgets 添加到DropDownMyDropDown.open() 调用现在包括 selfButton)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 2022-11-19
      相关资源
      最近更新 更多