我试图在python中实现一个方法来注册我的TextInputs的每个ID。
我是kivy的新手,有很多事我不理解。 我的测试代码如下:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class KivyGuiApp(App):
def build(self):
return root_widget
''' interface for global widget access '''
global_widgets = {}
def register_widget(self, widget_object):
print(widget_object.gid)
def print_widgets(self):
for w in self.global_widgets:
print(w)
class MyBox(BoxLayout):
pass
root_widget = Builder.load_string("""
MyBox:
gid: "4"
on_pos: app.register_widget(self)
name: 'screen_manager'
SimpleLayout:
<SimpleLayout@BoxLayout>:
gid: "1"
on_pos: app.register_widget(self)
name: 'simple_layout_rule'
TextInput:
gid: "inputB"
on_pos: app.register_widget(self)
TextInput:
gid: "inputC"
on_pos: app.register_widget(self)
TextInput:
gid: "inputD"
on_pos: app.register_widget(self)
""")
if __name__ == '__main__':
KivyGuiApp().run()
该代码的输出将是:
inputC
inputD
1
inputC
inputD
问题:
如果有人能帮助我理解为什么kivy会这样做,我会很高兴。
问候, 芬兰
答案 0 :(得分:1)
- 为什么跳过MyBox的gid“4”
醇>
您每次更改位置时都会调用register_widget()
函数,此外,任何小部件的默认位置都是(0, 0)
位置,而MyBox
则是properties
root,它永远不会改变位置,所以register_widget永远不会被调用。
- 为什么我可以简单地为每个小部件创建变量“gid”?
醇>
在Kivy language
中(0, 0)
的创建使用了该语法,在您的情况下,您创建的是StringProperty
。
- 为什么跳过gid“inputB”。如果我要删除第一个TextInput(因此总是第一个不会显示),也会对inputC进行此操作。
醇>
第一个小部件将不会显示,因为布局中第一个小部件的位置具有位置BoxLayout
,因此on_pos
不会改变其位置,因此永远不会触发MyBox
要验证我在说什么,让我们在Clock
的帮助下更改from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.clock import Clock
class MyBox(BoxLayout):
def __init__(self, **kwargs):
BoxLayout.__init__(self, **kwargs)
Clock.schedule_once(self.on_change, 1)
def on_change(self, *args):
print("=====================")
print("change position")
print("=====================")
self.pos = (100, 100)
root_widget = Builder.load_string("""
MyBox:
gid: "4"
on_pos: app.register_widget(self)
name: 'screen_manager'
SimpleLayout:
<SimpleLayout@BoxLayout>:
gid: "1"
on_pos: app.register_widget(self)
name: 'simple_layout_rule'
TextInput:
gid: "inputB"
on_pos: app.register_widget(self)
TextInput:
gid: "inputC"
on_pos: app.register_widget(self)
TextInput:
gid: "inputD"
on_pos: app.register_widget(self)
""")
class KivyGuiApp(App):
def build(self):
return root_widget
def register_widget(self, widget_object):
print(widget_object.gid)
if __name__ == '__main__':
KivyGuiApp().run()
的位置:
inputC
inputD
1
inputC
inputD
=====================
change position
=====================
4
1
inputB
inputC
inputD
获得以下输出:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
在印象中,观察到当根的位置改变时,所有小部件的位置都会改变。
答案 1 :(得分:1)
我尝试使用on_show
事件但未成功,因此我使用了on_draw
事件。以下是删除了on_pos
引用并实施on_draw
的代码:
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class KivyGuiApp(App):
def build(self):
return root_widget
''' interface for global widget access '''
global_widgets = {}
def register_widget(self, widget_object):
print(widget_object.gid)
def print_widgets(self):
for w in self.global_widgets:
print(w)
class MyBox(BoxLayout):
pass
root_widget = Builder.load_string("""
MyBox:
gid: "4"
name: 'screen_manager'
SimpleLayout:
<SimpleLayout@BoxLayout>:
gid: "1"
name: 'simple_layout_rule'
TextInput:
gid: "inputB"
TextInput:
gid: "inputC"
TextInput:
gid: "inputD"
""")
def on_draw(*args):
app = App.get_running_app()
for widget in root_widget.walk():
app.register_widget(widget)
if __name__ == '__main__':
Window.bind(on_draw=on_draw)
KivyGuiApp().run()