gpt4 book ai didi

python - 如何在kivy中动态创建屏幕内的布局?

转载 作者:行者123 更新时间:2023-11-30 22:33:25 24 4
gpt4 key购买 nike

我编写了这段 python 代码来在屏幕内动态创建 BoxLayout。

from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

class ListScreen(Screen):
def __init__(self,**kwargs):
super(ListScreen, self).__init__(**kwargs)
layout = BoxLayout(orientation ='vertical')
top_buttons=BoxLayout()
layout.add_widget(top_buttons)
top_buttons.add_widget(Button(text='Save'))

class ExampleApp(App):
def build(self):
root=ScreenManager()
root.add_widget(ListScreen(name='list'))
return root


ExampleApp().run()

它运行时没有任何编译错误,但输出只是一个空白屏幕。

最佳答案

问题是您尚未将 layout 添加到 ListScreen 实例中:

from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

class ListScreen(Screen):
def __init__(self,**kwargs):
super(ListScreen, self).__init__(**kwargs)
layout = BoxLayout(orientation ='vertical')
self.add_widget(layout) #<<<<<<<<<<<<<<<<<
top_buttons=BoxLayout()
layout.add_widget(top_buttons)
layout.add_widget(Button(text='Save'))

class ExampleApp(App):
def build(self):
root=ListScreen()
root.add_widget(ListScreen(name='list'))
return root


ExampleApp().run()

关于python - 如何在kivy中动态创建屏幕内的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45163457/

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