簡體   English   中英

Kivy-在屏幕中添加元素

[英]Kivy - Adding elements in a Screen

import os,sys,random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.graphics import Color, Rectangle
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen , SlideTransition
from kivy.animation import Animation
from kivy.properties import StringProperty

class Page(Screen):
    source = StringProperty()

class Imglayout(FloatLayout):

    def __init__(self,**args):
        super(Imglayout,self).__init__(**args)

        with self.canvas.before:
            Color(0,0,0,0)
            self.rect=Rectangle(size=self.size,pos=self.pos)


        self.rootpath = os.path.dirname(os.path.realpath(sys.argv[0]))

        self.images=[]

        for img in os.listdir(self.rootpath+'/images'):
            self.images.append(self.rootpath+'/images/'+img)

        self.index=random.randint(0,len(self.images)-1)

        self.im=Image(source=self.images[self.index])
        self.im.keep_ratio= False
        self.im.allow_stretch = True
        #self.add_widget(self.im)

        self.sm = ScreenManager(transition=SlideTransition())

        self.page1=Page(name='page1', source = self.images[self.index])
        self.page2=Page(name='page2', source = self.images[self.index+1])

        self.sm.add_widget(self.page1)
        self.sm.add_widget(self.page2)


        self.bind(size=self.updates,pos=self.updates)

    def updates(self,instance,value):
        self.rect.size=instance.size
        self.rect.pos=instance.pos

    def on_touch_down(self,touch):
            if self.collide_point(*touch.pos):
                if(self.sm.current == 'page1'):
                    next='page2'
                    page=self.page2
                else:
                    next='page1'
                    page=self.page1

                self.index=(self.index+1)%len(self.images)

                page.source = self.images[self.index]
                page.background.scale = 1.0

                self.sm.transition=SlideTransition()
                self.sm.current = next

                anim = Animation(
                    scale=page.background.scale*1.3, 
                    duration=15.0
                )

                anim.start(page.background)

                return True


            return False



class MainTApp(App):


    def build(self):


        root = BoxLayout(orientation='vertical',spacing=10)
        c = Imglayout()
        root.add_widget(c)

        cat=Button(text="Categories",size_hint=(1,.05))
        cat.bind(on_press=self.callback)
        root.add_widget(cat);

        return root

    def callback(self,value):
        print "CALLBACK CAT"


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

這里得到一些提示,我做了上面的程序。 它說在我的代碼和所引用的代碼中Page都沒有background屬性。 由於沒有背景屬性,因此這很明顯。 我認為它是繼承自Screen的。 我正在嘗試制作幻燈片。 但是我找不到有關如何設置屏幕背景的任何信息。

如果我用.background注釋掉所有內容並運行該應用程序。 單擊黑色空間,然后我開始在終端上連續收到此錯誤

[ERROR             ] [OSC         ] Address 127.0.0.1:3333 already in use, retry
 in 2 second 

而且我仍然沒有在應用程序上獲得任何背景。(全黑),如果我在touich函數上打印self.sm.current。 然后我發現它總是page1,它永遠不會改變。

Kivy Guide解釋了如何為小部件添加背景。 簡而言之,您可以使用以下代碼在Python中進行操作,該代碼綁定到小部件的位置和大小,以確保背景隨小部件一起移動。

with widget_instance.canvas.before:
    Color(0, 1, 0, 1) # green; colors range from 0-1 instead of 0-255
    self.rect = Rectangle(size=layout_instance.size,
                          pos=layout_instance.pos)
widget_instance.bind(size=self._update_rect, pos=self._update_rect)

...
def _update_rect(self, instance, value):
    self.rect.pos = instance.pos
    self.rect.size = instance.size

或者,您可以使用kv語言更自然地進行操作,例如

MyWidget:
    canvas.before:
        Color:
            rgba: 0, 1, 0, 1
        Rectangle:
            # self here refers to the widget i.e screen
            pos: self.pos
            size: self.size

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM