滚动视图中的奇异果背景 [英] Kivy background in scroll view

查看:72
本文介绍了滚动视图中的奇异果背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试应用程序摘要:我正在编写一个具有可滚动视图(名为Scroller)和许多要查看的字段(名为Field)的Kivy应用.有时很难区分这些单独的字段,因此我决定为每个字段使用交替的背景色以帮助彼此区分.我的测试应用程序使用20个单独的字段,每个字段在深灰色和深灰色之间交替.

Summary of test application: I am writing a Kivy app with a scrollable view (named Scroller) with many fields (named Field) to look at. These separate fields are really difficult to distinguish on occasion, so I decided to use alternating background colors for each field to help distinguish each other. My testing application uses 20 individual fields each of which alternates between dark grey and darker grey.

测试试验: 启动应用程序,该程序看起来很棒.交替的背景看起来很好.即使当我向下滚动时,应用程序看起来也不错.但是,当我向上滚动应用程序时,该应用程序似乎变得奇怪.文本随应用程序滚动,但背景不滚动.更好的(讽刺的是)文本开始淡入他们的邻居背景.当我再次向下滚动(超过最远的滚动点)时,问题似乎消失了.

Testing trials: Starting the application, the program looks great. The alternating background appear just fine. Even when I scroll down the application looks fine. However, the application seems to get bizarre when I scroll up on the application. The text scrolls with the application, but the background does not. Even better (sarcastically), the text starts to fade away into their neighbors background. The problem just seems to vanish when I scroll down again (passed the point of the furthest scroll up point).

简要问题描述:Field的背景颜色"在事件向上滚动时弄乱了应用程序.

Brief problem description: The Field's "background color" messes up the application during scrolling up events.

旁注:我还注意到,滚动太多后,应用程序变得有些迟钝.我对Kivy的绘制周期不太熟悉,但是淡化背景不应导致过快的减速.

Side note: I have also noticed that the application got a little sluggish after scrolling too much. I am not that familiar with the drawing cycle of Kivy, but blitting backgrounds should not yield an excessive slowdown.

测试应用程序:

import kivy
kivy.require('1.0.7')

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.graphics import Color, Rectangle

class Main(App):
    def build(self):
        self.root = GridLayout(rows = 1)
        self.root.add_widget(Scroller())
        return self.root

class Scroller(ScrollView):
    def __init__(self):
        ScrollView.__init__(self)
        self.view = GridLayout(cols = 1, size_hint = (1, None))
        self.add_widget(self.view)
        self.view.bind(minimum_height = self.view.setter('height'))

        for i in range(20):
            self.view.add_widget(Field('Test field {}'.format(i),i%2 is 0))

class Field(GridLayout):
    def __init__(self, name, bg):
        assert isinstance(name, str)
        assert isinstance(bg, bool)
        self.bg = bg
        GridLayout.__init__(self,
                            rows = 1,
                            padding = 10,
                            size = (0, 60),
                            size_hint = (1, None))
        self.add_widget(Label(text = name))
        self.add_widget(Button(text = 'Test button',
                               size = (200, 0),
                               size_hint = (None, 1)))
        self.bind(pos = self.change_background)
        self.bind(size = self.change_background)

    def change_background(self, *args):
        with self.canvas.before:
            if self.bg:
                Color(0.2, 0.2, 0.2, mode = 'rgb')
            else:
                Color(0.1, 0.1, 0.1, mode = 'rgb')
            Rectangle(pos = self.pos, size = self.size)

if __name__ in ('__main__', '__android__'):
    app = Main()
    app.run()

推荐答案

def change_background(self, *args):
        self.canvas.before.clear()#<- clear previous instructions
        with self.canvas.before:
            if self.bg:
                Color(0.2, 0.2, 0.2, mode = 'rgb')
            else:
                Color(0.1, 0.1, 0.1, mode = 'rgb')
            Rectangle(pos = self.pos, size = self.size)

每次字段的位置/大小更改时,您都在画布上添加/堆积说明,而不会清除前面的说明.

You are adding/piling instructions to the canvas every time the Field's position/size changes, without clearing the previous instructions.

您还应该考虑使用kv,因为它不只是一小段代码,最终可以为您节省大量时间.您可以像这样使用kv来转换代码::

You should also look into using kv as for anything more than a small snippet it ends up saving you a lot of time. You can convert you code using kv like so ::

import kivy
kivy.require('1.0.7')

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.properties import ObjectProperty, BooleanProperty
from kivy.lang import Builder

Builder.load_string('''
<Scroller>
    # root is Scroller here
    # create a new ObjectProperty in kv that holds the ref to Gridlayout
    # so you can access the instance in python code
    view: glayout
    GridLayout:
        id: glayout
        cols: 1
        size_hint: (1, None)
        height: self.minimum_height

<Field>
    canvas.before:
        Color:
            rgba: (0.2, 0.2, 0.2, 1) if self.bg else (0.1, 0.1, 0.1, 1)
        Rectangle:
            # binding properties is done implicitly and instructions aren't
            # piled up while doing that.
            pos: self.pos
            # self here refers to Field as `self` is supposed to refer to the
            # Widget not the drawing instruction
            size: self.size
    rows: 1
    padding: 10
    size: (0, 60)
    size_hint: (1, None)
    Label:
        text: root.name
    Button:
        text: 'test button'
        size: (200, 0)
        size_hint: (None, 1)
''')


class Main(App):

    def build(self):
        self.root = GridLayout(rows = 1)
        self.root.add_widget(Scroller())
        return self.root


class Scroller(ScrollView):
    def __init__(self, **kwargs):
        super(Scroller, self).__init__(**kwargs)
        for i in range(20):
            # access self.view that was set in kv
            self.view.add_widget(
                                Field(
                                    name = 'Test field {}'.format(i),
                                    bg = i%2 is 0))

class Field(GridLayout):

    # use  kivy's Properties so it becomes easier to observe and apply changes
    # as a plus these can also be directly used in kv. As a advantage of using this now
    # you can change name and bg dynamically and the changes should be reflected on
    # screen
    name = ObjectProperty('Test field uninitialized')

    bg = BooleanProperty(False)


if __name__ in ('__main__', '__android__'):
    app = Main()
    app.run()

这篇关于滚动视图中的奇异果背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆