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

时间:2019-04-13 21:25:23

标签: python python-3.x user-interface kivy kivy-language

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

但是由于某些原因,仅显示标题,并且屏幕不希望转换。我没有任何错误。

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

这是我的错误代码:

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上显示。有人可以给我一些建议,以使我的App像上面解释的那样起作用吗?

1 个答案:

答案 0 :(得分:2)

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

相关问题