2017-07-21 119 views
0

在Kivy中,我試圖在第一頁上拉第二頁後覆蓋另一頁。此時,列表中的單詞有重疊,因爲這些頁面看起來很透明,而第二頁不包含第一頁。我試圖改變小部件的背景顏色,但無濟於事,甚至嘗試在某個階段添加背景圖像。請幫忙,這已經佔用了我足夠的時間。如果可能的話,我會很感激純Python代替kv語言的解決方案。例如:Kivy頁面佈局覆蓋另一頁以防止重疊

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() 

回答

0

一個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() 
+0

感謝@Mox您的解決方案的工作,但我發現了一塊我失蹤了只用蟒蛇與layout.canvas.add(顏色(0,1,0) )和layout.canvas.add(Rectangle(size =(100,100)))。它們在ScrollView中工作效果最好,以防有人在循環播放時遇到問題 – Jeff