为什么屏幕没有加载Kv语言?

问题描述 投票:1回答:1

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

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

我尝试了不同的布局作为我的Apps主类继承,包括FloatLayoutStackLayoutBoxLayout。我还用AnchorLayout修复了标题,并使用不同的布局作为ScreenManager的内联布局。如果我删除ScreenManager我看到我的小部件,但当然,我无法过渡。我最初尝试使用TabbedPanel来容纳我不同的小部件,但是如果我添加了太多的小部件(但现在不是这样),我遇到了一个常量的RefError: weak object reference。因此,我重新设计了一些我知道在以前的应用程序上工作的东西,尽管不那么复杂。

这是我错误的代码:

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()

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

python python-3.x user-interface kivy kivy-language
1个回答
2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.