如何将屏幕背景设置为猕猴桃图像 [英] How do I set the screen background to image in kivy

查看:69
本文介绍了如何将屏幕背景设置为猕猴桃图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在此代码中将背景更改为图像,而不使用任何.Kv文件.我想将屏幕背景设置为图像,但只能看到带有.kv文件的

How do I change the background to image in this code am not using any .Kv file. I want to set the screen background to an image but am only seeing the ones with .kv file

import kivy
from kivy.app import App
from kivy.uix.floatlayout import Floatlayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.label import Label 


class LandingScreen(FloatLayout):
    def __init__(self, **kwargs):
        super(LandingScreen, self).__init__(**kwargs)

         self.score=0

        # put whatever pos_hint value you want.          
        self.add_widget(Label(text='SCORE: ' + str(score), size_hint=(0.5, 0.5)))
        self.btn1=Button(text='button1 ', size_hint=(0.5, 0.5), 
        on_press=self.click_b1))
        self.btn2=Button(text='button2', size_hint=(0.5, 0.5), 
        on_press=self.click_b2))


            
        self.add_widget(self.btn1)
        self.add_widget(self.btn2)

        def click_b1(self, instance):
             
             score +=10
        def click_b2(self, instance):
             score += 10
       
class SplashApp(App):
    def build(self):
        return LandingScreen()

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

推荐答案

kv中更容易实现,但是您可以通过将以下代码添加到LandingScreen__init__()方法中而无需kv来做到这一点:

It's easier to do in kv, but you can do it without kv by adding this code to the __init__() method of LandingScreen:

    with self.canvas.before:
        self.bg = Rectangle(pos=self.pos, size=self.size, source='background.png')

LandingScreen的大小更改时,您还需要调整背景的大小.为此,请将此方法添加到LandingScreen:

And you will also need to adjust the size of the background when the size of LandingScreen changes. To do this add this method to LandingScreen:

def on_size(self, *args):
    self.bg.size = self.size

这篇关于如何将屏幕背景设置为猕猴桃图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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