我正在尝试使用一些自定义小部件构建一个 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 基本上占据了整个窗口.但是,当我尝试用我的自定义按钮替换 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?
此外,是否有必要以这种方式创建自定义按钮?kivy 教程使用这种方法来制作他们的自定义按钮,但我不能只做这样的事情
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
放在了 kv 文件中的一个 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
规则来解决这个问题,就像你在 <RootWidget>
规则中所做的那样,或者实际上通常更容易使用 BoxLayout(即子类 BoxLayout
而不是 Widget
)当然可以免费自动调整大小/定位.
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) 中,按钮行为已被抽象为处理检测触摸和调度 on_press
的 ButtonBehavior
类等适当.Behavior 不是小部件,因此您可以将其与任何其他小部件子类化以将任何东西变成按钮!
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!