gpt4 book ai didi

python - 添加 - 删除 kivy 中的小部件

转载 作者:太空宇宙 更新时间:2023-11-04 10:07:24 25 4
gpt4 key购买 nike

我在 kivy 中添加或删除小部件时遇到了一些困难。是这样的:

主窗体应包含三个小部件中的两个,Widget1、Widget2 和 Widget3。按下 Widget1 的按钮,Widget2 应该被移除,Widget3 应该出现。

这是 main.py 文件:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.factory import Factory

class TableApp(App):

def on_pause(self): return True

def on_resume(self): pass

class Form(BoxLayout):
def click(self, instance, arg):
print 'this is the Form class'
print 'this is my arg ... ', arg
print 'this is the button pressed ... ', instance
print 'these are the children of the Form class:', self.children
Form().remove_widget(Widget2)
Form().add_widget(Widget3)

class Widget1(BoxLayout):

def click(self, instance, arg):
print 'this is the Widget 1'
print 'this is my arg ... ', arg
print 'this is my instance', instance, '\n'
Factory.Form().click(instance,arg)

class Widget2(BoxLayout):
pass

class Widget3(BoxLayout):
pass

if __name__ in ('__android__', '__main__'):
TableApp().run()

这是 .kv 文件:

#:import Factory kivy.factory.Factory
Form:

<Form>:
orientation: 'vertical'

Widget1:
Widget2:

<Widget1>:
Button:
text: "Widget 1 Button"
on_press: root.click(self, 'arg')

<Widget2>:
Button:
text: 'Widget 2 Button'

<Widget3>:
Button:
text: 'Widget 3 Button'

在类 Form 中,我检查 Widgets1 和 2 是否是该类的 child :

print 'these are the children of the Form class:', self.children

我得到:

these are the children of the Form class: [<__main__.Widget2 object at 0x7fe5833317c0>, <__main__.Widget1 object at 0x7fe5833316e0>]

因此,当我尝试删除现有子项并添加新子项时,我得到:

TypeError: descriptor 'unbind' of 'kivy._event.EventDispatcher' object needs an argument

有人可以帮忙吗?谢谢。

最佳答案

大多数问题的发生是因为您创建了新的小部件而不是使用现有的小部件。

这是一个工作示例(查看评论):

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.factory import Factory #no need for this


class Form(BoxLayout):
def click(self, instance, arg):
print ('this is the Form class')
print ('this is my arg ... ', arg)
print ('this is the button pressed ... ', instance)
print ('these are the children of the Form class:', self.children)
#notice! here we use self, not Form() which will make a new Form instance
self.remove_widget(self.children[0]) #should be Widget2
self.add_widget(Widget3()) #Here we really want a new widget

class Widget1(BoxLayout):

def click(self, instance, arg):
print ('this is the Widget 1')
print ('this is my arg ... ', arg)
print ('this is my instance', instance, '\n')
self.parent.click(instance,arg) #changed to use the existing From instance instead of tring to create a new one

class Widget2(BoxLayout):
pass

class Widget3(BoxLayout):
pass



class TableApp(App):


def on_pause(self): return True

def on_resume(self): pass

if __name__ in ('__android__', '__main__'):
TableApp().run()

关于python - 添加 - 删除 kivy 中的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40326783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com