简体   繁体   English

如何在Kivy中更改背景颜色

[英]How to change background colour in Kivy

I need a bit of a hand with a program I'm trying to code using kivy and python 3, however I'm relatively new to both.我需要对我尝试使用 kivy 和 python 3 进行编码的程序有所了解,但是我对两者都比较陌生。 What I need in my program is to setup two different background colours, that the user can switch between (a night mode, and one to use in daylight)我在我的程序中需要的是设置两种不同的背景颜色,用户可以在它们之间切换(一种夜间模式,一种在白天使用)

#globalvariable
backgroundcolour = [50, 50, 50]

class MainScreen(Screen):

    rgb = StringProperty()
    rgb = backgroundcolour

    def changebackground(self):
        self.canvas.clear()
        backgroundcolour = [55, 5, 99]
        print("DONE")

Kivy file: Kivy 档案:

<MainScreen>:
    name: 'main'
    canvas:
        Color: 
            rgb: root.rgb

However all I get after I run the changebackground subroutine, my kivy window just replaces itself with a blank black screen.然而,在我运行 changebackground 子例程后,我得到的所有信息,我的 kivy window 只是用一个空白的黑屏替换了自己。 I presume what I'm doing wrong is I'm not refreshing the window, or something, but I've got no idea how to go about doing that.]我想我做错的是我没有刷新 window 或其他东西,但我不知道如何 go 这样做。]

Many thanks非常感谢

canvas:
    Color: 
        rgb: root.rgb

After this part you have to draw something that will cover the widget background:在这部分之后,您必须绘制一些将覆盖小部件背景的东西:

Rectangle:
    size: self.size
    pos: self.pos

or in your changebackground() :或在您的changebackground()

with self.canvas:
    Color(rgb=self.rgb)  # rgba might be better
    Rectangle(size=self.size, pos=self.pos)

which is probably more optimal if you intend to use it when changing the color styles not so often.如果您打算在不经常更改颜色样式时使用它,这可能更理想。 And the best thing would be using canvas.before , especially if you have a widget that draws something (eg Button ).最好的办法是使用canvas.before ,特别是如果您有一个可以绘制某些东西的小部件(例如Button )。

Also, the color is in range 0 - 1 , therefore your color will be some kind of really bright purple-ish something.此外,颜色在0 - 1范围内,因此您的颜色将是某种非常明亮的紫色。 And just a note: this will change only the widget's background, therefore your Window background will still be the default one (currently black).请注意:这只会更改小部件的背景,因此您的窗口背景仍将是默认背景(当前为黑色)。 For this to change you'll need to use Window.clearcolor .为此,您需要使用Window.clearcolor进行更改。

This is a pure python code on how to put background on layout, no Kivy design language这是一个关于如何在布局上放置背景的纯 python 代码,没有 Kivy 设计语言

Code of App class not include App类的代码不包括

For future reference:备查:

from kivy.uix.gridlayout import GridLayout
from kivy.graphics.vertex_instructions import Rectangle
from kivy.graphics.context_instructions import Color

from kivy.uix import button

class MyLayout(GridLayout):
    def __init__(self, **kwargs):
        super(MyLayout, self).__init__(**kwargs)
        self.cols = 1

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

        with self.canvas.before:
            Color(.20, .06, .31, 1)
            self.rect = Rectangle(
                size=self.size,
                pos=self.pos
            )

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

样本

Note: To play with the color: my rgb values are (46, 14, 71), then just divide it with 255 (.20, .06, .31, 1).注意:要使用颜色:我的 rgb 值为 (46, 14, 71),然后将其除以 255 (.20, .06, .31, 1)。 The last value is the alpha, 1 is 100%最后一个值是 alpha,1 是 100%

Hope it can help you guys.希望能帮到大家。 Just up vote to help others who's also looking the answer.只需投票以帮助其他也在寻找答案的人。

Solution to your problem解决您的问题

Add this code init:添加此代码初始化:

self.daylightBtn = button.Button(text="Day Light")
self.daylightBtn.bind(on_press=self.daylight_bg)
            
self.nightlightBtn = button.Button(text="Night Light")
self.nightlightBtn.bind(on_press=self.nighlight_bg)
            
self.add_widget(self.daylightBtn)
self.add_widget(self.nightlightBtn)

Button event:按钮事件:

   def daylight_bg(self, instance):
        with self.canvas.before:
            Color(1, 1, 1, 1)
            self.rect = Rectangle(
                size=self.size,
                pos=self.pos
            )


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

In .kv file.kv文件中

canvas.before:
        Color:
            rgba: (128, 0, 128, 1)
        Rectangle:
            size: self.size
            pos: self.pos

Hope to help you希望能帮到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM