gpt4 book ai didi

python - 为什么屏幕无法以 Kv 语言加载?

转载 作者:行者123 更新时间:2023-12-01 01:02:28 26 4
gpt4 key购买 nike

我有一个应用程序,其固定标题中带有三个切换按钮,该标题是屏幕管理器的外部缩进布局。初始化时,导入屏幕必须显示,即 self.ids.scrn_man.current = 'import_scn",按下切换按钮时,下一个屏幕应显示,即 on_state: scrn_man.current = "settings_scrn”

但由于某种原因,仅显示标题,并且屏幕不想转换。我没有收到任何错误。

我尝试了不同的布局作为我的 Apps 主类继承,包括 FloatLayoutStackLayoutBoxLayout。我还使用 AnchorLayout 固定标题,并使用不同的布局作为 ScreenManager 的内联布局。如果我删除 ScreenManager 我会看到我的小部件,但当然,我无法转换。我一开始尝试使用 TabbedPanel 来容纳不同的小部件,但如果我添加了太多小部件(但现在还不是),我会遇到一个持续的 RefError:弱对象引用。因此,我用我知道可以在以前的应用程序上使用的东西重新设计,尽管不那么复杂。

这是我的错误代码:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.stacklayout import StackLayout
from kivy.uix.togglebutton import ToggleButton
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.properties import StringProperty, ObjectProperty

Builder.load_string("""
<RoundedButton@Button>:
background_color: 0,0,0,0
canvas.before:
Color:
rgba: (.47,.47,.47,1) if self.state=='normal' else (1,.6,0,1)
RoundedRectangle:
pos: self.pos
size: self.size
radius: [8,]
<RoundedCancelButton@Button>:
background_color: 0,0,0,0
canvas.before:
Color:
rgba: (.47,.47,.47,1) if self.state=='normal' else (1,.2,.2,1)
RoundedRectangle:
pos: self.pos
size: self.size
radius: [8,]
<RoundedAcceptButton@Button>:
background_color: 0,0,0,0
canvas.before:
Color:
rgba: (.47,.47,.47,1) if self.state=='normal' else (.2,1,.6,1)
RoundedRectangle:
pos: self.pos
size: self.size
radius: [8,]

<TabbedContainer@ToggleButton>:
background_color: (1, .5, 0, 1)
background_normal: ''
size_hint_y: None
height: 50
size_hint_x: (1 / 3)
spacing: 30
<Tab>:
canvas.before:
Color:
rgba: (.89, .89, .89, 1)
Rectangle:
size: self.size
pos: self.pos
orientation: 'lr-tb'
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: 30
canvas.before:
Color:
rgba: (1, .3, 0, 1)
Rectangle:
size: self.size
pos: self.pos
Label:
text: 'Test'
color: (1, 1, 1, 1)
size_hint_x: 1
StackLayout:
orientation: 'lr-tb'
Label:
text: ''
size_hint_x: 1
size_hint_y: None
height: 10
TabbedContainer:
id: import_tog
text: 'Import'
state: 'down'
group: 'admin_navs'
on_state: root.change_screen(self)
TabbedContainer:
id: calculate_tog
text: 'Calculate'
group: 'admin_navs'
on_state: root.change_screen(self)
TabbedContainer:
id: settings_tog
text: 'Settings'
group: 'admin_navs'
on_state: root.change_screen(self)
BoxLayout:
id: ui_content
padding: 10
ScreenManager: #Problem here I think
id: scrn_man
Screen:
id: import_scrn
name: 'import_scrn'
StackLayout:
orientation: 'lr-tb'
Label:
text: ''
size_hint_x: 1
Label:
text: ''
size_hint_x: 0.2
RoundedButton:
text: 'Choose File'
size_hint_x: 0.2
TextInput:
id: get_file
readonly: True
size_hint_x: 0.5
Label:
text: ''
size_hint_x: 0.1
Label:
text: ''
size_hint_x: 0.2
RoundedButton:
text: 'Import'
size_hint_x: 0.2
Label:
text: ''
size_hint_x: 0.6
StackLayout:
id: import_data_content
orientation: 'lr-tb'
size_hint_y: None
height: 90
Screen:
id: calculate_scrn
name: 'calculate_scrn'
Screen:
id: settings_scrn
name: 'settings_scrn'
StackLayout:
orientation: 'lr-tb'
size_hint_x: 0.5
Label:
text: ''
size_hint_x: 0.1
Button:
text: 'Add Employee'
size_hint_x: 0.2
Label:
text: ''
size_hint_x: 0.2
Button:
text: 'CSV'
size_hint_x: 0.2
Label:
text: ''
size_hint_x: 0.3
BoxLayout:
orientation: 'horizontal'
size_hint_x: 0.5
Label:
text: 'In Time'
size_hint_x: 0.7
TextInput:
size_hint_x: 0.3
Label:
text: 'Out Time'
size_hint_x: 0.7
TextInput:
size_hint_x: 0.3
""")
class TabbedContainer(ToggleButton):
pass

class FileChoosePopup(Popup):
load = ObjectProperty()

class RoundedButton(Button):
pass
class RoundedCancelButton(Button):
pass
class RoundedAcceptButton(Button):
pass

class Tab(StackLayout):
file_path = StringProperty("No file chosen")
the_popup = ObjectProperty(None)
def __init__(self, **kwargs):
super().__init__(**kwargs)

#load import window on initialisation
import_window = self.ids.import_scrn
self.ids.scrn_man.current = 'import_scrn'


def change_screen(self, instance):
if instance.text == 'Import':
self.ids.scrn_man.current = 'import_scrn'
elif instance.text == 'Calculate':
self.ids.scrn_man.current = 'calculate_scrn'
else:
self.ids.scrn_man.current = 'settings_scrn'

class TestApp(App):
def build(self):
return Tab()

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

我希望导入屏幕必须在初始化时显示,并且屏幕在切换按钮状态:向下上转换。有人可以给我一些关于如何使我的应用程序按上述方式运行的建议吗?

最佳答案

您的屏幕正在根据您的设置正确加载。您需要检查您的整个 kv 字符串,查看您的 size_hint 设置。检查包含子项的每个项目,并确保其子项的 size_hint_x 总和小于或等于 1.0,并且 size_hint_y 的总和相同。

关于python - 为什么屏幕无法以 Kv 语言加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55670015/

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