使用自定义小部件kivy [英] Use custom widgets kivy

查看:179
本文介绍了使用自定义小部件kivy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一些自定义小部件来构建kivy应用。但是,每当我尝试使用它们时,它们就永远无法与我的布局一起使用。使用普通按钮:

I'm trying to build a kivy app using some custom widgets. However whenever I try to use them they never work with my layout. Using a normal button:

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ListProperty

class RootWidget(Widget):pass

class myApp(App):

    def build(self):
        global rw
        rw  = RootWidget()
        return rw

if __name__ == '__main__':
    myApp().run()

#:kivy 1.8.0

<RootWidget>:

    BoxLayout:
        size: root.size
        orientation: 'horizontal'
        spacing: 10
        padding: 10

        Button:
            id: abut
            text: "Custom Button"

预期,我的Button基本上会占据整个窗口。但是,当我尝试用自定义按钮替换按钮

This works as expected, my Button basically takes up the entire window. However when I try replacing the Button with my custom button

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ListProperty

class MyWidget(Widget):

     pressed = ListProperty([0, 0])

     def on_touch_down(self, touch):
         if self.collide_point(*touch.pos):
             self.pressed = touch.pos
             return True
         return super(MyWidget, self).on_touch_down(touch)

     def on_pressed(self, instance, pos):
         print ('pressed at {pos}'.format(pos=pos))

class RootWidget(Widget):pass

class someApp(App):

    def build(self):
        global rw
        rw  = RootWidget()
        return rw

if __name__ == '__main__':
    someApp().run()

#:kivy 1.8.0

<MyWidget>:
    BoxLayout:
        orientation: 'horizontal'
        spacing: 10

        Button:
            id: abut
            text: "Custom Button"        

<RootWidget>:

    BoxLayout:
        size: root.size
        orientation: 'horizontal'
        spacing: 10
        padding: 10

        MyWidget:

它仅显示在窗口的左下角,不会显示表现得像个按钮。我想念什么?

it only appears in the bottom left-hand corner of the window and doesn't behave like a button. What am I missing?

此外,甚至有必要以这种方式创建自定义按钮吗?千篇一律的教程使用了这种方法来制作自定义按钮,但我不能只做类似的事情

Furthermore, is it even necessary to create a custom button this way? The kivy tutorials used this sort of method to make their custom button but can't I just do something like this

Button:
    on_press: root.do_action()

使每个按钮的行为有所不同吗?

to make each button behave differently?

推荐答案

您的实际问题是,尽管您的 MyWidget 放置在<$ c $ kv文件中的c> BoxLayout ,其子元素 BoxLayout 不会 not 将其大小设置为 MyWidget 大小,因此只需保持屏幕左下方的默认大小和位置(100,100)

Your actual problem is that although your MyWidget is placed in a BoxLayout in the kv file, its child BoxLayout does not have its size set to the MyWidget size, and therefore just maintains the default size and position of (100, 100) in the bottom left of the screen.

您可以通过给它额外的 size:root.size 规则来解决此问题,就像在<$ c $中一样c>< RootWidget> 规则,或者实际上通常使用BoxLayout会更容易(例如,子类 BoxLayout 而不是小部件),它当然会为您免费提供自动调整大小/位置。

You can fix this by giving it the extra size: root.size rule as you did within the <RootWidget> rule, or actually normally it's easier to just use a BoxLayout (i.e. subclass BoxLayout instead of Widget) which of course gives you the automatic resizing/positioning for free.

另外,正如Joran所说的那样,如果您只想在按下按钮时执行某项操作,则可以使用第二种方法...这就是您想要做的!我不知道您要看的是哪个示例,但通常不需要像您这样的复杂安排。

Also, as Joran said, if you just want to do something when the button is pressed you can use the second method...that's what you're intended to do! I don't know what example you are looking at, but you wouldn't normally need a complex arrangement like yours.

您可能还想知道最新版本(1.8)将按钮行为抽象为 ButtonBehavior 类,该类处理检测触摸并调度 on_press 等适当地。 行为不是小部件,因此您可以将其与其他任何小部件进行子类化,以将任何内容制成按钮!

You might also be interested to know that in the most recent release (1.8) the button behavior has been abstracted to a ButtonBehavior class that handles detecting touches and dispatching on_press etc. appropriately. The Behavior isn't a widget, so you can subclass it with any other widget to make anything into a button!

这篇关于使用自定义小部件kivy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆