嗨,我想在没有 kv 文件的情况下更改 kivy 进度条的颜色,我该怎么做?
这是我在网上找到的片段:
from kivy.app import App
from kivy.uix.progressbar import ProgressBar
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.graphics import Color, Rectangle
class CustomProgressBar(ProgressBar):
def __init__(self, **kwargs):
super(CustomProgressBar, self).__init__(**kwargs)
# Set initial color
self.color = [1, 0, 0, 1] # Red color
def set_color(self, color):
self.color = color
class MyApp(App):
def build(self):
layout = BoxLayout(orientation='vertical')
self.progress_bar = CustomProgressBar(max=100)
layout.add_widget(self.progress_bar)
# Change color after 2 seconds
Clock.schedule_once(self.change_color, 2)
return layout
def change_color(self, dt):
# Change the color of the progress bar
self.progress_bar.set_color([0, 1, 0, 1]) # Green color
if __name__ == '__main__':
MyApp().run()
使用函数
Clock.schedule_once()
和 set_color()
将颜色更改为您喜欢的颜色,并且不要忘记更改 RGBA 值或间隔