首页 > 解决方案 > Kivy 1.10.1 滑块复制自身

问题描述

我目前正在尝试创建一个简单的 Slider 来控制我的应用程序中的文本大小。我遇到的问题是,即使滑块按我的意图运行,它似乎在第一个不能移动的滑块下方创建了另一个版本。您可以在此处查看提供的图像中的外观[替代文本:显示 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 还很陌生,但我以前玩过滑块,直到现在才遇到这些问题。

标签: pythonpython-3.xkivykivy-language

解决方案


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

您可以尝试删除此行:

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

并更改此行:

return look

至:

pass

推荐阅读