kivy:装饰setter函数(EventDispatcher)

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

以下 kv 代码将图像放置在按钮内并根据按钮的大小对其进行缩放:

   Button:                                                                          
    text:'Katze'                                                                 
    background_color: ('#00ffff')                                                
    font_size: 32                                                                
    on_press:root.playSound(self)                                                
    Image:                                                                       
        source: './katze.png'                                                    
        center_x: self.parent.center_x                                           
        center_y: self.parent.center_y                                           
        size: root.width*0.25, root.height*0.25

有没有办法在Python中重现这个?

在这篇文章的帮助下我有一个近似的解决方案 kivy 小部件:多个按钮,居中,获取窗口/屏幕尺寸,现在我的问题是:

我可以修饰图像对象的 setter 函数(如下),以便可以更改长度和宽度参数吗?例如,其价值的四分之一?我是Python新手,花了一些时间尝试理解装饰器原理,但尚未找到解决方案。

def build(self):
    self.layout = GridLayout(cols=3, spacing=10)
    for animal in self.zoo:
        knopf = ButtonID(
            id = animal, #member ButtonID(Button)
            background_color=get_color_from_hex(self.zoo[animal][0]),
            on_press =  self.playSound,                             
        )
        self.layout.add_widget(knopf)
        
        bild=Image()
        knopf.add_widget(bild)
        bild.source=self.animal[tier][2]
                                                               
        knopf.bind(center=bild.setter('center'))
        #knopf.bind(width=bild.setter('width'))                                                     
        #knopf.bind(height=bild.setter('height'))                                                   
        knopf.bind(size=bild.setter('size'))

    return self.layout

您好, 迈克尔

kivy decorator kivy-language
1个回答
0
投票

研究了装饰器概念后,我找到了解决方案。这是自动生成的带有嵌入图像的按钮的源代码,这些按钮总是通过绑定和装饰器很好地缩放。

该程序显示带有动物图标的按钮。当您按下按钮时,会播放带有相应动物声音的声音文件。作为 kivy 编程课程的动力。很有趣。

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.utils import get_color_from_hex
from kivy.core.audio import SoundLoader
from kivy.lang import Builder

import sys

class ButtonID(Button):
    def __init__(self, id, **kwargs):
        super(Button, self).__init__(**kwargs)
        self.id=id

    def update(self, *kargs):
        print("groesse: ", self.size)
        print("position: ", self.pos)
        #self.canvas.clear() 
        with self.canvas:
            pass
        
class ZooApp(App):

    zoo = {"Vogel":["00FFFF", "./birds_cardinal.mp3","./vogel.png"],
           "Katze":["DDA0DD", "./cat_meow_04.mp3","./katze.png"],
           "Kuh":["E2DDD5", "./cows_two_cows_mooing.mp3","./kuh.png"],
           "Esel":["FFFF00", "./donkey_braying.mp3","./esel.png"],
           "Schwein":["FFD700", "./pig_grunt.mp3","./schwein.png"],
           "Schaf":["FF8C00", "./sheep_bleat_001.mp3","./schaf.png"],
           "Lanz":["E2DDD5","./lanz_bulldog_trim.mp3","./lanzFrei.png"]}

    def playSound(self, button):
        SoundLoader.load(filename=self.zoo[button.id][1]).play()
        
    def build(self):
        self.title = "Kleintier-Zoo"
        self.layout = GridLayout(cols =3, spacing = 10) 
        for tier in self.zoo:
            knopf = ButtonID(
                id = tier,
                background_color = get_color_from_hex(self.zoo[tier][0]),
                on_press =  self.playSound, # play-sound = callback-fkt
            )

            self.layout.add_widget(knopf)

            bild=Image()

            knopf.add_widget(bild)
            bild.source=self.zoo[tier][2]
                
            def dekorateur(func):
                def wrapper(*args, **kwargs):
                    erstes=args[0] #scheint button-id zu sein
                    zweites=args[1]
                    zweites=zweites/2
                    args=(erstes,zweites)
                    print("args: ", erstes, zweites)
                    print("kwargs: ", kwargs)
                    return func(*args, **kwargs)
                return wrapper

            knopf.bind(center=bild.setter('center'))
            
            setterfunktion = bild.setter('width')
            setterfunktion=dekorateur(setterfunktion)
            knopf.bind(width=setterfunktion)

            setterfunktion = bild.setter('height')
            setterfunktion=dekorateur(setterfunktion)
            knopf.bind(height=setterfunktion)

            
        return self.layout
        
if __name__ == "__main__":
    ZooApp().run()
© www.soinside.com 2019 - 2024. All rights reserved.