Kivy 1.10.1 Slider 自我复制

Kivy 1.10.1 Slider duplicating itself

我目前正在尝试创建一个简单的滑块来控制我的应用程序中文本的大小。我 运行 遇到的问题是,尽管滑块按我的预期运行,但它似乎在第一个无法移动的滑块下方创建了另一个版本。您可以在提供的图片中看到它的外观 here [alt text: 显示 kivy 滑块的基本用户界面的屏幕截图。滑块已向前移动,在其后方有另一个副本位于默认位置](如您所见,BoxLayout 中的标签文本也重叠了)。我目前正在使用 Kivy 1.10.1 和 Python 3.7.2.

这是我的 Python 脚本:

 # -*- coding: utf-8 -*-
import kivy
kivy.require('1.10.1')
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.actionbar import ActionBar
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.graphics.vertex_instructions import (Rectangle, Ellipse, Line)
from kivy.graphics.context_instructions import Color
from kivy.uix.checkbox import CheckBox
from kivy.uix.slider import Slider

#Window.size = (360/1.2,740/1.2)

class HomeScreen(Screen):
    pass

class OptionsScreen(Screen):
    pass

class TutorialScreen(Screen):
    pass

class ScreenController(ScreenManager):
    pass

look = Builder.load_file('main.kv')

class MainApp(App):
    def build(self):
        return look

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

这里是 Kivy 语言的相关部分,用于在我的

上创建滑块
<OptionsScreen>
name: 'option'

BoxLayout:
    orientation:'vertical'
    BoxLayout:
        orientation:'horizontal'
        size_hint_y: 1/3
        Label:
            text:'Text size'
            font_size: textsize.value
            size_hint_x:.5
        Slider:
            id:textsize
            min: 5
            max: 25
            value:15
            step: 1
            size_hint_x:.5

不过,如果您想阅读整个 Kivy 语言文档,我也将其张贴在这里。

    #: import FadeTransition kivy.uix.screenmanager.FadeTransition
# Reference main.py
#: import main main
#: import Slider kivy.uix.slider
#: import ActionBar kivy.uix.actionbar
#: import Window kivy.core.window
ScreenController:
    transition: FadeTransition()
    HomeScreen:
    OptionsScreen:
    TutorialScreen:

<HomeScreen>
    name: 'home'
    BoxLayout:
        id:'hem'
        orientation:'vertical'
        BoxLayout:
            size_hint_x: 1
            orientation:'horizontal'
            canvas:
                Color:
                    rgba: 0,1,0,1
                Rectangle
                    size: self.size
                    pos: self.pos
        BoxLayout:
            orientation:'horizontal'
            BoxLayout:
                size_hint_x: .5
                orientation:'horizontal'
                canvas:
                    Color:
                        rgba: 1,0,1,1
                    Rectangle
                        size: self.size
                        pos: self.pos
            BoxLayout:
                size_hint_x: .5
                orientation:'horizontal'
                canvas:
                    Color:
                        rgba: 1,1,0,1
                    Rectangle
                        size: self.size
                        pos: self.pos

    ActionBar:
        pos_hint: {'top':1}

        ActionView:
            use_separator: True
            ActionPrevious:
                title: 'Fredde & Kribbas kivy'
                with_previous: False
            ActionOverflow:
            ActionGroup:
                text: 'Group1'
                ActionButton:
                    text: 'home'
                    on_touch_down: app.root.current = 'home'
                ActionButton:
                    text: 'Options'
                    on_touch_down: app.root.current = 'option'
                ActionButton:
                    text: 'Tutorial'
                    on_touch_down: app.root.current = 'tut'

<OptionsScreen>
    name: 'option'

    BoxLayout:
        orientation:'vertical'
        BoxLayout:
            orientation:'horizontal'
            size_hint_y: 1/3
            Label:
                text:'Text size'
                font_size: textsize.value
                size_hint_x:.5
            Slider:
                id:textsize
                min: 5
                max: 25
                value:15
                step: 1
                size_hint_x:.5
        BoxLayout:
            #fontsize checkbox
            orientation:'horizontal'
            size_hint_y: 1/3
            BoxLayout:
                orientation:'vertical'
                Label:
                    text: 'Nightmode'
                CheckBox:
                    id:default
                    size_hint_y: None
                    active: True
                    height:'50dp'
                    group:'g1'
            BoxLayout:
                orientation:'vertical'
                Label:
                    text: 'Daymode'

                CheckBox:
                    id:stor
                    size_hint_y: None
                    height:'50dp'
                    group:'g1'
        BoxLayout:
            orientation:'horizontal'
            size_hint_y: 1/3
            canvas:
                Color:
                    rgba: 0,0,1,1
                Rectangle
                    size: self.size
                    pos: self.pos

    ActionBar:
        pos_hint: {'top':1}
        ActionView:
            use_separator: True
            ActionPrevious:
                title: 'Fredde & Kribbas kivy'
                with_previous: False
            ActionOverflow:

            ActionGroup:
                text: 'Group1'
                ActionButton:
                    text: 'home'
                    on_touch_down: app.root.current = 'home'
                ActionButton:
                    text: 'Options'
                    on_touch_down: app.root.current = 'option'
                ActionButton:
                    text: 'Tutorial'
                    on_touch_down: app.root.current = 'tut'
<TutorialScreen>
    name: 'tut'

    ActionBar:
        pos_hint: {'top':1}
        ActionView:
            use_separator: True
            ActionPrevious:
                title: 'Fredde & Kribbas kivy'
                with_previous: False
            ActionOverflow:

            ActionGroup:
                text: 'Group1'
                ActionButton:
                    text: 'home'
                    on_touch_down: app.root.current = 'home'
                ActionButton:
                    text: 'Options'
                    on_touch_down: app.root.current = 'option'
                ActionButton:
                    text: 'Tutorial'
                    on_touch_down: app.root.current = 'tut'

有谁知道我为什么会遇到这个问题?我对 kivy 还很陌生,但我以前玩过滑块,直到现在才遇到这些问题。

您不需要在 python 代码中加载您的 kv 文件,因为您已经将您的 kv 文件命名为 main.kv.

你可以试试去掉这一行:

look = Builder.load_file('main.kv')

并更改此行:

return look

至:

pass