我正在使用循环创建27个标签实例。这些标签需要使用Label.text方法编号 - 我已经实现了。Kivy矩形重绘,self.pos和self.size是(0,0)
我还需要能够使用python函数(基于其他输入)为这些标签(画布矩形)重新着色。 这是不起作用
基本上该位,GridLayout的的INIT方法创建custButton()标签的29个实例。此循环还重新编号(label.text)按钮,但重新着色一个标签不起作用。
我已经分离出self.pos和self.size都在xBackground_Color函数返回(0,0) - 这就是为什么旧的清除后,新矩形不画(清作品)。
为什么self.pos和self.size不能从python方面工作?
我试过soooooooo很多东西!如果可以的话请帮忙。
这里的kvMain.kv:
#:kivy 1.0.9
FloatLayout:
canvas:
Color:
rgba: (64/255, 64/255, 64/255, 1) #Whole screen background
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
padding: 10
FloatLayout: #Dummy widget
myGrid:
<myGrid>:
cols: 9
spacing: 3
padding: 3
canvas.before:
Color:
rgba: (216/255, 216/255, 216/255, 1)
Rectangle:
pos: self.pos
size: self.size
<custButtom>:
canvas.before:
Color:
rgba: (191/255, 191/255, 191/255, 1)
Rectangle:
pos: self.pos
size: self.size
font_size: 14
color: (90/255, 90/255, 90/255, 1)
text: 'Er' #Don't ever want to see this text. Will set each label in loop
size: self.texture_size
这里的main.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.graphics.vertex_instructions import Rectangle, Line
from kivy.graphics.context_instructions import Color
IN_Labels = {}
class custButtom(Label):
def __init__(self, **kwargs):
super(custButtom, self).__init__(**kwargs)
def xtext(self, newText):
self.text = newText
self.size = self.texture_size
def xbackground_color(self, newR, newG, newB, newA):
print('IN_Labels[14] pos ('+ str(IN_Labels[14].pos[0]) + ',' + str(IN_Labels[14].pos[1]) + ') size ('+ str(IN_Labels[14].size[0]) + ',' + str(IN_Labels[14].size[1]) + ')')
print('self pos ('+ str(self.pos[0]) + ',' + str(self.pos[1]) + ') size ('+ str(self.size[0]) + ',' + str(self.size[1]) + ')')
self.canvas.before.clear()
with self.canvas.before:
Color(newR, newG, newB, newA)
#Problem is that self.size and self.pos are both (0,0)
Rectangle(size=self.size, pos=self.pos)
class myGrid(GridLayout):
def __init__(self, **kwargs):
super(myGrid, self).__init__(**kwargs)
for i in range(0, 27, 1):
IN_Labels[i] = custButtom()
self.add_widget(IN_Labels[i])
#CAN change text, needs more work to get it right
IN_Labels[i].xtext(str(i))
# I need to be able to change backgroundcolor programatically
IN_Labels[14].xbackground_color(1,0,0,1)
class MainApp(App):
def build(self):
return kvMain
kvMain = Builder.load_file("kvMain.kv")
if __name__ == '__main__':
MainApp().run()