【问题标题】:Kivy page layout cover one page over the other to prevent overlappingKivy 页面布局将一页覆盖在另一页上以防止重叠
【发布时间】:2017-07-23 05:55:37
【问题描述】:

在 Kivy 中,在将第二页拉到第一页上之后,我试图将第一页覆盖在另一页上。目前列表中的单词重叠,因为页面看起来是透明的,而第二页没有覆盖第一页。我尝试更改小部件的背景颜色但无济于事,甚至尝试在某个阶段添加背景图像。请帮助这已经占用了我足够多的时间。如果可能的话,我会很感激纯 python 而不是 kv 语言的解决方案。示例:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.pagelayout import PageLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle

# Window.clearcolor = (252, 235, 233, 0)

class MyScreenManager(ScreenManager):
    pass

class PageLayoutScreen(Screen):
    pass

class PageLayout1(PageLayout):
    def __init__(self, **kwargs):
        super(PageLayout1, self).__init__(**kwargs)

        myList = ['hello there', 'hello to you too']
        t = 0
        for i in myList:
            nameWdgt = Label(text="[b]" + myList[t] + "[/b]", markup=True, font_size='15sp')
            locationWdgt = Label(text="[b]" + myList[t] + "[/b]", markup=True, font_size='15sp')
            self.add_widget(nameWdgt)
            self.add_widget(locationWdgt)

            # below is my attempt to cover the first item in the list "hello there" when we pull the second item in the list over it "hello to you too"
            with self.canvas.before:
                Color(0, 1, 0, 1)  # green; colors range from 0-1 instead of 0-255
                self.profile_layout = Rectangle(size=self.size, pos=self.pos)
            t += 1

root_widget = Builder.load_string('''
#:import NoTransition kivy.uix.screenmanager.NoTransition
#:import sys sys
MyScreenManager:
    transition: NoTransition()

    PageLayoutScreen:

<PageLayoutScreen>:
    name: 'test'
    PageLayout1
''')

class ScreenManagerApp(App):
    def build(self):
        return root_widget

ScreenManagerApp().run()

【问题讨论】:

    标签: android python kivy page-layout


    【解决方案1】:

    Label 默认情况下是透明的,而不是纹理(文本),这就是您看到重叠的原因。您创建背景的尝试只是绘制了一个具有默认大小的矩形。您需要为每个标签绘制背景以查看发生了什么。

    编辑:我不确定您使用 ScreenMangerScreen 想要完成什么,所以我将它们排除在外。

    这是一个例子:

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.pagelayout import PageLayout
    from kivy.uix.label import Label
    from kivy.properties import NumericProperty
    
    Builder.load_string("""
    <MyLabel>:
        font_size: "15sp"
        markup: True
        canvas.before:
            Color:
                rgba: self.bg
                rgba: [[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [.5, .5, .5, 1]][self.pg_index]
            Rectangle:
                size: self.size
                pos: self.pos
    
    """)
    
    
    class MyLabel(Label):
        pg_index = NumericProperty(None)
    
    
    class PageLayout1(PageLayout):
        def __init__(self, *args, **kwargs):
            super(PageLayout1, self).__init__(*args, **kwargs)
    
            myList = ['hello there', 'hello to you too']
            pg_index = 0
            for i in myList:
                nameWdgt = MyLabel(text="[b]" + myList[0] + "[/b]", pg_index=pg_index)
                pg_index += 1
    
                locationWdgt = MyLabel(text="[b]" + myList[1] + "[/b]", pg_index=pg_index)
    
                pg_index += 1
    
                self.add_widget(nameWdgt)
                self.add_widget(locationWdgt)
    
    
    class ScreenManagerApp(App):
        def build(self):
            return PageLayout1()
    
    
    if __name__ == '__main__':
        ScreenManagerApp().run()
    

    【讨论】:

    • 感谢@Mox,您的解决方案有效,但我发现仅使用带有 layout.canvas.add(Color(0., 1., 0)) 和 layout.canvas 的 python 缺少一块。添加(矩形(大小=(100, 100)))。它们在 ScrollView 内工作得最好,以防有人在循环它们时遇到问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 2014-06-11
    • 1970-01-01
    • 2021-01-13
    • 2014-07-23
    • 2018-01-27
    相关资源
    最近更新 更多