添加和清除小部件时的Kivy布局问题

2024-10-01 15:47:44 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用widget命令add_widget和clear_widgets时,我得到的widgets本身看起来很好,在屏幕的左下角非常小。在

我要在这里解释我所有的步骤,请你耐心听我说。在

我有一个系统可以从一个小部件切换到另一个部件。 “Widget”基本上是整个表单、基本画布和布局。在

据我所知,这些必须有一个基地布局。因此,首先我在中放入一个根元素,然后用这个根初始化我的UI管理器。在

class MyApp(App):
    rootForm = BoxLayout()
    ui = UserInterface(rootForm)
    return rootForm

我的UI处理小部件的切换。它在创建时将所有小部件保存在一个列表中。它还为自己提供了一个对用户界面的引用,用于回调目的。在

^{pr2}$

ChangeWidget是这里真正的核心,它清除了根widget并添加了一个新的。在

def ChangeWidget(self, inWidgetName):
    self.__RootWidget.clear_widgets() # Clear out the widgets
    self.__RootWidget.add_widget( self.__WidgetDictionary[inWidgetName] ) # Add our new widget

这显示我的表单很好,有一个按钮。当单击按钮时,它附加了一个回调函数,用于将窗体从窗体1更改为窗体2。在

#FirstWidget.py
class FirstWidget(BoxLayout):
    __UserInterfaceReference = None

    def SetUIReference(self, inUserInterface):
        self.__UserInterfaceReference = inUserInterface

    def ChangeWidget(self, inWidgetName):
        self.__UserInterfaceReference.ChangeWidget(inWidgetName)

相应的.kv文件

#FirstWidget.kv
<FirstWidget>:      
    BoxLayout:
        orientation: 'vertical'
        padding: 50
        spacing: 10
    Button:
        on_release: root.ChangeWidget('form2')

一旦单击按钮,就会发生错误。 格式1和格式2几乎相同。Form2比Form2有一个butotn。 Form1和Form2的代码完全相同,只是类名不同。 同样,在它自己的环境中,form2看起来很完美。在

以下是该漏洞的截图: AddedWidgetMessedUp

编辑: 目前,我转而使用屏幕管理器:http://kivy.org/docs/api-kivy.uix.screenmanager.html


Tags: selfadd部件def窗体widgetswidget按钮
1条回答
网友
1楼 · 发布于 2024-10-01 15:47:44

如果运行以下代码,请将上面的代码合并到一个文件中:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

Builder.load_string(
'''
<FirstWidget>:      
    BoxLayout:
        orientation: 'vertical'
        padding: 50
        spacing: 10
    Button:
        text: 'change to form 2'
        on_release: root.ChangeWidget('form2')

<SecondWidget>:
    TextInput:
    BubbleButton:
        text: 'change to form 1'
        on_release: root.ChangeWidget('form1')
''')

class UserInterface():
    __WidgetDictionary = {}
    __RootWidget = None

    def __init__(self, inRootWidget):
        self.__RootWidget = inRootWidget # Keep track of our root
        # Generate two forms to be swapped in/out
        self.__WidgetDictionary["form1"] = FirstWidget()
        self.__WidgetDictionary["form1"].SetUIReference(self)

        self.__WidgetDictionary["form2"] = SecondWidget()
        self.__WidgetDictionary["form2"].SetUIReference(self)
        self.ChangeWidget("form1")

    def ChangeWidget(self, inWidgetName):
         # Clear out the widgets
        self.__RootWidget.clear_widgets()
         # Add our new widget
        self.__RootWidget.add_widget(self.__WidgetDictionary[inWidgetName])


class FirstWidget(BoxLayout):
    __UserInterfaceReference = None

    def SetUIReference(self, inUserInterface):
        self.__UserInterfaceReference = inUserInterface

    def ChangeWidget(self, inWidgetName):
        self.__UserInterfaceReference.ChangeWidget(inWidgetName)

class SecondWidget(BoxLayout):
    __UserInterfaceReference = None

    def SetUIReference(self, inUserInterface):
        self.__UserInterfaceReference = inUserInterface

    def ChangeWidget(self, inWidgetName):
        self.__UserInterfaceReference.ChangeWidget(inWidgetName)


class MyApp(App):

    def build(self):
        rootForm = BoxLayout()
        ui = UserInterface(rootForm)
        return rootForm


if __name__ == '__main__':
    MyApp().run()

每件事都像它应该做的那样。由于您没有提供完整的代码,因此很难确定您的错误所在并给出解释。试着看看我发布的代码和你自己的代码之间的区别,以确定你在做什么不同。在

希望这有帮助。在

相关问题 更多 >

    热门问题