python - 试图在屏幕布局中调用布局类 - kivy
问题描述
本质上,我有多个屏幕,由屏幕管理器控制(主要用于登录),但是在主屏幕上,我将它分解为标题、导航栏和正文部分。在正文部分,我希望能够调用我有类的不同布局,并在按下导航栏上的按钮时添加和删除这些布局。
我使用 kivy 来控制 gui 的各个方面,但对它来说还很陌生。目前我有:
Builder.load_string('''
<MainMenu>:
FloatLayout:
canvas.before:
Color:
rgba: 0.235, 0.271, 0.302, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
rows: 4
Label:
size_hint: 1,0.12
text: "ProductName"
text_size: self.size
halign: 'left'
valign: 'middle'
font_size: 32
color: [0.114,0.18,0.224,1]
StackLayout:
size_hint: 1,0.10
orientation: 'lr-tb'
#Purchase Order System
Button:
id: butpos
text: "Purchase Order"
size_hint: 0.166,1
on_press: spaceholder.add_widget(pos)
#Asset Assignment System
Button:
id: butaas
text: "Asset Assignment"
size_hint: 0.166,1
#Review and Revise System
Button:
id: butrrs
text: "Review"
size_hint: 0.166,1
#Administrative System
Button:
id: butadm
text: "Administration"
size_hint: 0.166,1
#Analytics and Reporting System
Button:
id: butars
text: "Analytics"
size_hint: 0.166,1
#Placeholder, possibly documentation
Button:
id: butbut
text: "Placeholder"
size_hint: 0.166,1
Label:
canvas.before:
Color:
rgba: 0.259, 0.643, 0.937, 1
Rectangle:
pos: self.pos
size: self.size
size_hint: 1,0.005
Widget:
id: spaceholder
<PurchaseOrder>
id: pos
orientation: 'lr-tb'
#Asset Details
Label:
BoxLayout:
orientation: 'vertical'
Label:
text: "Asset Requirements"
Label:
text: "Asset1: "
TextInput:
id: as1count
Label:
text: "Asset2: "
TextInput:
id: as2count
''')
class MainMenu(Screen):
pass
class PurchaseOrder(StackLayout):
pass
class MainApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(MainMenu(name='mainmenu'))
return sm
if __name__ == '__main__':
MainApp().run()
PurchaseOrder 是一个我喜欢添加或删除的类,就像我打算为其他导航选项设置的类一样。我尝试了很多方法,但我认为我错过了一些东西。我要么被“pos”抛出未定义,要么当我尝试不同的组合时“spaceholder”具有错误的属性。
解决方案
您可以通过修改Purchase Order
Button
:
on_press: spaceholder.add_widget(Factory.PurchaseOrder())
这需要添加到您kv
的导入Factory
:
#:import Factory kivy.factory.Factory
我建议您使用 aLayout
来spaceholder
利用它们的尺寸和定位功能。例如,使用AnchorLayout
自动居中其子级(但仅限于单个子级)。像这样:
AnchorLayout:
id: spaceholder
推荐阅读
- c# - 不理解 JSON Web Tokens 的东西
- reactjs - 在 setState React 和 socketIO 中发布了重复的字母
- flutter - 使用 widget.[variable_name] 是引用 StatefulWidget 变量的最佳方式吗?
- java - 无法作为客户端连接到服务器时不会抛出异常
- javascript - 需要帮助将工作箭头函数转换为常规函数以编写单元测试
- python - 我在硒中找不到这个词
- javascript - 如何有效地通过 React 中父级的引用访问子级?
- mongoose - 按日期对所有子文档进行分组并获取每个日期的最后数据
- swift - how do I actually display a variable in SwiftUI to the end user
- javascript - 在 setTimeout() 中调用对象方法