我想在kivy中动态更改网格布局中小部件的颜色 [英] I want to dynamically change the color of widget in grid Layout in kivy
本文介绍了我想在kivy中动态更改网格布局中小部件的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想动态更改网格布局中小部件的颜色.
我怎样才能做到这一点?
我创建了一个6x6的小部件矩阵,并且需要动态闪烁网格布局中的图块.
I want to dynamically change the color of a widget in grid layout.
How can i achieve that?
I create a widget matrix of 6x6 and i need to blink the tiles in the grid layout dynamically.
推荐答案
尝试创建网格小部件,然后仅使用预定功能更改小部件的颜色.下面的代码现在可能很完美,但可能会帮助您:
Try creating grid widgets and then just change color of the widgets with a scheduled function . Below code might now be perfect but may help you :
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.graphics import Color
from kivy.clock import Clock
import random
class RootWidget(GridLayout):
pass
class MainApp(App):
def build(self):
parent = GridLayout(cols=6)
Colour=[0,0,0,0]
self.create_button(parent,Colour,1,1)
Clock.schedule_interval(lambda a:self.update(parent), 1)
return parent
def update(self,obj):
print "I am update function"
obj.clear_widgets()
print "random value is ",random.random()
for i in (1,2,3,4,5,6):
for j in (1,2,3,4,5,6):
c=[random.random(),random.random(),random.random(),random.random()]
d=[i,j]
self.create_button(obj,c,i,j)
def create_button(self,obj,color,i,j):
a=Button(background_color=color,text='Hello World %s%s'%(i,j))
obj.add_widget(a)
if __name__ == '__main__':
MainApp().run()
这篇关于我想在kivy中动态更改网格布局中小部件的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文