【发布时间】:2015-09-24 15:00:13
【问题描述】:
我正在尝试创建一个自定义 kivy 小部件,但是当我在我的测试应用程序中渲染它时,会出现一个空白屏幕。我试过跟踪代码调用,看起来所有的初始化和绘图方法都被调用了(我在小部件代码下面包含了打印语句的终端输出。我还包括我的测试应用程序,但这很简单并且是我用于我已经创建的几个工作应用程序的相同外壳)。我的应用程序主循环开始,但没有显示任何内容。为什么我没有看到我的小部件?
[编辑]所以我已经让 NodeEnd 对象进行渲染,并进行了一些重大的结构更改,将大量代码带入了 .kv 语言。我正在用较新的版本替换帖子上的代码。不幸的是,它们之间的标签仍然没有渲染
nodewidget.kv
#: kivy 1.9.0
<NodeEnd>
canvas:
Color:
rgba: self.color[0], self.color[1], self.color[2], self.color[3]
Ellipse:
pos: self.center
size: self.width / 2, self.height / 2
angle_start: self.degree_range[0]
angle_end: self.degree_range[1]
<NodeWidget>
left: left
body: body
right: right
GridLayout:
cols: 3
NodeEnd:
id: left
degree_range: 180, 360
on_pressed: self.PressedLeft(self, args[0])
on_moved: self.MovedLeft(self, args[0])
on_released: self.ReleasedLeft(self, args[0])
on_color: self.LeftSwitch(self, args[0])
NodeBody:
id: body
text: self.title
on_pressed: self.PressedBody(self, args[0])
on_moved: self.MovedBody(self, args[0])
on_released: self.ReleasedBody(self, args[0])
on_pressed_flag: self.BodySwitch(self, args[0])
NodeEnd:
id: right
degree_range: 0, 180
on_pressed: self.PressedRight(self, args[0])
on_moved: self.MovedRight(self, args[0])
on_released: self.ReleasedRight(self, args[0])
on_color: self.RightSwitch(self, args[0])
NodeWidget3.py
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ListProperty, StringProperty, BooleanProperty, ObjectProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.graphics import *
from kivy.lang import Builder
Builder.load_file('nodewidget.kv')
class NodeWidget(GridLayout):
title = StringProperty('')
pressed_body = ListProperty([0, 0])
released_body = ListProperty([0, 0])
moved_body = ListProperty([0, 0])
switch_body = BooleanProperty(False)
pressed_left = ListProperty([0, 0])
released_left = ListProperty([0, 0])
moved_left = ListProperty([0, 0])
switch_left = BooleanProperty(False)
pressed_right = ListProperty([0, 0])
released_right = ListProperty([0, 0])
moved_right = ListProperty([0, 0])
switch_right = BooleanProperty(False)
left = ObjectProperty(None)
body = ObjectProperty(None)
right = ObjectProperty(None)
def __init__(self, **kwargs):
super(NodeWidget, self).__init__(**kwargs)
def PressedBody(self, *args):
self.pressed_body = args
def MovedBody(self, *args):
self.moved_body = args
def ReleasedBody(self, *args):
self.released_body = args
def BodySwitch(self, *args):
pass
def PressedLeft(self, *args):
self.pressed_left = args
def MovedLeft(self, *args):
self.moved_left = args
def ReleasedLeft(self, *args):
self.released_left = args
def LeftSwitch(self, *args):
pass
def PressedRight(self, *args):
self.pressed_right = args
def MovedRight(self, *args):
self.moved_right = args
def ReleasedRight(self, *args):
self.released_right = args
def RightSwitch(self, *args):
pass
class NodeBody(Label):
#on_color is triggered on down or up on the button and move off or on the button
#it will return the initial values when the button is not pressed
#when the button is pressed, this will return True
pressed_flag = BooleanProperty(False)
#on_pressed is triggered when the button is initially hit
pressed = ListProperty([0, 0])
#on_released is triggered when the button is released
released = ListProperty([0, 0])
#on_moved is triggered when the touch is moved and the touch is not taken outside the button
moved = ListProperty([0, 0])
title = StringProperty('')
def __init__(self, **kwargs):
super(NodeBody, self).__init__(**kwargs)
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
self.pressed = touch.pos
self.pressed_flag = True
return touch
return super(NodeBody, self).on_touch_down(touch)
def on_touch_move(self, touch):
if self.collide_point(*touch.pos):
self.moved = touch.pos
self.pressed_flag = True
return touch
self.pressed_flag = False
return super(NodeBody, self).on_touch_move(touch)
def on_touch_up(self, touch):
if self.collide_point(*touch.pos):
self.released = touch.pos
self.pressed_flag = False
return touch
return super(NodeBody, self).on_touch_up(touch)
class NodeEnd(Widget):
#on_color is triggered on down or up on the button and move off the button
#it will return the initial values when the button is not pressed
#when the button is pressed, this will return [0.8, 0.2, 0.2, 1]
#Not meant to be updated via code, only generate events
color = ListProperty([0.7, 0.1, 0.1, 0.8])
#[90, 270] for left
#[270, 90] for right
degree_range = ListProperty([0, 0])
#on_pressed is triggered when the button is initially hit
pressed = ListProperty([0, 0])
#on_released is triggered when the button is released
released = ListProperty([0, 0])
#on_moved is triggered when the button is moved and the touch is not taken outside the button
moved = ListProperty([0, 0])
def __init__(self, **kwargs):
super(NodeEnd, self).__init__(**kwargs)
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
self.pressed = touch.pos
self.color = [0.8, 0.2, 0.2, 1]
return touch
return super(NodeEnd, self).on_touch_down(touch)
def on_touch_move(self, touch):
if self.collide_point(*touch.pos):
self.moved = touch.pos
self.color = [0.8, 0.2, 0.2, 1]
return touch
self.color = [0.7, 0.1, 0.1, 0.8]
self.released = touch.pos
return super(NodeEnd, self).on_touch_move(touch)
def on_touch_up(self, touch):
if self.collide_point(*touch.pos):
self.released = touch.pos
self.color = [0.7, 0.1, 0.1, 0.8]
return touch
return super(NodeEnd, self).on_touch_up(touch)
测试应用:
nodeeditor.kv
#kivy 1.9.0
<NodeEditorWidget>:
GridLayout:
size: root.size
cols: 3
NodeWidget:
title: 'Test 1'
on_pressed_body: app.SelectNode(self, args[0])
on_moved_body: app.MoveNode(self, args[0])
on_released_body: app.DeselectNode(self, args[0])
on_pressed_left: app.SelectLeft(self, args[0])
on_moved_left: app.MoveLeft(self, args[0])
on_released_left: app.DeselectLeft(self, args[0])
on_pressed_right: app.SelectRight(self, args[0])
on_moved_right: app.MoveRight(self, args[0])
on_released_right: app.DeselectRight(self, args[0])
NodeTest.py
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty, StringProperty
from kivy.lang import Builder
from NodeWidget3 import NodeWidget
Builder.load_file('nodeeditor.kv')
class NodeEditorWidget(Widget):
pass
class NodeEditorApp(App):
def build(self):
return NodeEditorWidget()
print('Node Editor initialized')
def SelectNode(self, *args):
pass
def MoveNode(self, *args):
pass
def DeselectNode(self, *args):
pass
def SelectLeft(self, *args):
pass
def MoveLeft(self, *args):
pass
def DeselectLeft(self, *args):
pass
def SelectRight(self, *args):
pass
def MoveRight(self, *args):
pass
def DeselectRight(self, *args):
pass
if __name__ == '__main__':
NodeEditorApp().run()
【问题讨论】:
标签: python user-interface widget kivy