[英]Python Kivy - change Label color
Is there a way to change the background_color
of a Label
without using canvas? 有没有一种方法可以在不使用画布的情况下更改Label
的background_color
?
As i run the code, the background color is automaticlly black... 当我运行代码时,背景颜色会自动变成黑色。
Here is my simple code: 这是我的简单代码:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
class MyWindow(App):
def build(self):
box = BoxLayout()
label = Label(text='Hello World')
box.add_widget(label)
return box
window = MyWindow()
window.run()
Thank you 谢谢
Background color by itself, no. 背景颜色本身,否。 You'll still need to change it inside another widget or something similar. 您仍然需要在另一个小部件或类似的内部更改它。 But if you use a picture of one color, then it is! 但是,如果您使用一种颜色的图片,那就可以了!
Label
doesn't have a background by itself, that's why you can use its canvas
to put it there, otherwise it's transparent. Label
本身没有背景,这就是为什么您可以使用其canvas
将其放置在其中的原因,否则它是透明的。 If it's transparent, that means it can show a content of another widget eg the one that's under it. 如果它是透明的,则表示它可以显示另一个小部件的内容,例如其下面的小部件。
So put under it Image
and you have basically the whole canvas
+ Rectangle
with source
thing, but separated into two widgets. 因此,将其放在Image
,您基本上具有带source
的整个canvas
+ Rectangle
,但分为两个小部件。 If you want to change only background color, open eg mspaint
, fill it with a single color and load with Python. 如果您只想更改背景颜色,请打开例如mspaint
,用单一颜色填充它并加载Python。
It might not work with BoxLayout
correctly, because it handles position of its children automatically, but with FloatLayout
that's not a problem anymore: 它可能无法正确使用BoxLayout
,因为它会自动处理其子级的位置,但是使用FloatLayout
不再是问题:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
class MyApp(App):
def build(self):
flt = FloatLayout()
image = Image(size_hint=(None, None), size=(300, 300),
source=<path to image>)
label = Label(size_hint=(None, None), size=(300, 300),
text='Hello World')
flt.add_widget(image)
flt.add_widget(label)
return flt
MyApp().run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.