Kivy在屏幕中添加元素

2024-04-23 20:35:21 发布

您现在位置:Python中文网/ 问答频道 /正文

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

here得到一些提示,我做了上面的程序。它表示Page在我的代码和引用的代码中都没有background属性。很明显,因为没有背景属性。我以为它是从屏幕继承来的。我想做一个幻灯片之类的东西。但是我找不到任何关于如何设置屏幕背景的信息。在

如果我用.background注释掉所有内容并运行应用程序。点击黑色区域,然后我开始在终端上不断地得到这个错误

^{pr2}$

我仍然没有任何背景的应用程序(它都是黑色的) 如果我打印自同步电流关于touich函数。然后我发现它总是第1页,它从不改变。在


Tags: fromposimportselfsourcesizeindexdef
1条回答
网友
1楼 · 发布于 2024-04-23 20:35:21

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语言更自然地做

^{pr2}$

相关问题 更多 >