【发布时间】:2014-05-12 19:58:52
【问题描述】:
我正在尝试使用一些自定义小部件构建一个 kivy 应用程序。但是,每当我尝试使用它们时,它们都不会与我的布局一起使用。使用普通按钮:
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 时
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:
它只出现在窗口的左下角,不像一个按钮。我错过了什么?
此外,是否有必要以这种方式创建自定义按钮? kivy 教程使用这种方法来制作他们的自定义按钮,但我不能只做这样的事情
Button:
on_press: root.do_action()
让每个按钮的行为不同?
【问题讨论】:
-
是的,你可以只做第二种方法......(我喜欢尽可能避免使用 .kv 语言并直接在 python 中实现大部分这些东西)
-
@JoranBeasley 为什么要避免使用 kvlang?
-
我只是对一般的描述性语言有一种自然的反感(加上在 1.8 之前,.kv 文件中存在循环引用的一些内存泄漏)我更好地理解当我只是子类小部件时发生的事情python 并以这种方式构建我的东西
-
所有说 imho Kivy 是做跨平台 gui 东西的方式(尤其是针对平板电脑和手机)
标签: python python-2.7 button kivy