from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import random
from kivy.uix.label import Label
Builder.load_string('''
<CameraClick>:
orientation: 'vertical'
RelativeLayout:
name:"index_layout"
canvas:
Color:
rgba: 1,1,1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"parent_layout"
rows:1
GridLayout:
name:"left"
cols:1
GridLayout:
name:"left_1"
cols:1
canvas:
Color:
rgba: 0.5, 1, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"left_2"
cols:1
size_hint_y:5
canvas:
Color:
rgba: 1, 0, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"left_3"
cols:1
canvas:
Color:
rgba: 1, 1, 0.6, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"left_4"
cols:1
canvas:
Color:
rgba: 1, 0.9, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"center"
cols:1
size_hint_x:6
GridLayout:
name:"center_1"
rows:1
canvas:
Color:
rgba: 1, 0, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"center_2"
rows:1
size_hint_y:5
GridLayout:
name:"center_3"
rows:1
canvas:
Color:
rgba: 1, 0.5, 0.2, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"center_4"
rows:1
canvas:
Color:
rgba: 1, 0.3, 0.2, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"right"
rows:1
''')
line1=Builder.load_string("""
GridLayout:
name:"line1"
rows:1
canvas:
Color:
rgba: 0.5,0.5,0.3, 1
Line:
points:self.pos[0],self.pos[1]+30,self.pos[0],self.pos[1]+self.size[1]-30
width:5
""")
line2 = Builder.load_string("""
GridLayout:
name:"line2"
rows:1
canvas:
Color:
rgba: 0.5,0.5,0.3, 1
Line:
points:self.pos[0],self.pos[1]+2*self.size[1]+30,self.pos[0]+self.size[0],self.pos[1]+2*self.size[1]+30
width:5
""")
lines="""
GridLayout:
name:"{}"
rows:1
canvas:
Color:
rgba:{}
Line:
points:{}
width:15
cap:"square"
"""
class CameraClick(BoxLayout):
pass
class DrawBar():
def build(self):
y_labels= ("100", "80","60" , "40", "20","0")
x_labels= ("S","F","D","H","S","A","N","x","y","z")
printer=CameraClick()
# 绘制纵坐标
for text in y_labels:
printer.children[0].children[0].children[2].children[2].add_widget(Label(text=text))
# 标题
printer.children[0].children[0].children[1].children[3].add_widget(Label(text="title",halign="center"))
# 设置横纵坐标轴刻度
# line1
# 绘制坐标轴
printer.children[0].children[0].children[1].children[2].add_widget(line1)
printer.children[0].children[0].children[1].children[0].add_widget(line2)
# 使用for来控制条带的
line_color="{},{},{}, 1"
line_name="line3"
line1_points="self.pos[0]-15,self.pos[1]+30+15,self.pos[0]-15,self.pos[1]+self.size[1]*{}+15"
line_heithts=[0.4,0.5,0.8,0.1,0.3,0.6,0.7,0.4,0.9,0.5]
for _,line_height in zip(x_labels,line_heithts):
line3 = Builder.load_string(lines.format(line_name,line_color.format(random.random(),random.random(),random.random()),line1_points.format(line_height)))
printer.children[0].children[0].children[1].children[2].add_widget(line3)
# 绘制横坐标
for text in x_labels:
printer.children[0].children[0].children[1].children[1].add_widget(Label(text=text))
return printer
if __name__ == '__main__':
runTouchApp(DrawBar().build())
可以传递参数代码
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import random
from kivy.uix.label import Label
Builder.load_string('''
<CameraClick>:
orientation: 'vertical'
RelativeLayout:
name:"index_layout"
canvas:
Color:
rgba: 1,1,1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"parent_layout"
rows:1
GridLayout:
name:"left"
cols:1
GridLayout:
name:"left_1"
cols:1
canvas:
Color:
rgba: 0.5, 1, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"left_2"
cols:1
size_hint_y:5
canvas:
Color:
rgba: 1, 0, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"left_3"
cols:1
canvas:
Color:
rgba: 1, 1, 0.6, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"left_4"
cols:1
canvas:
Color:
rgba: 1, 0.9, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"center"
cols:1
size_hint_x:6
GridLayout:
name:"center_1"
rows:1
canvas:
Color:
rgba: 1, 0, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"center_2"
rows:1
size_hint_y:5
GridLayout:
name:"center_3"
rows:1
canvas:
Color:
rgba: 1, 0.5, 0.2, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"center_4"
rows:1
canvas:
Color:
rgba: 1, 0.3, 0.2, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"right"
rows:1
''')
line1=Builder.load_string("""
GridLayout:
name:"line1"
rows:1
canvas:
Color:
rgba: 0.5,0.5,0.3, 1
Line:
points:self.pos[0],self.pos[1]+30,self.pos[0],self.pos[1]+self.size[1]-30
width:5
""")
line2 = Builder.load_string("""
GridLayout:
name:"line2"
rows:1
canvas:
Color:
rgba: 0.5,0.5,0.3, 1
Line:
points:self.pos[0],self.pos[1]+2*self.size[1]+30,self.pos[0]+self.size[0],self.pos[1]+2*self.size[1]+30
width:5
""")
lines="""
GridLayout:
name:"{}"
rows:1
canvas:
Color:
rgba:{}
Line:
points:{}
width:15
cap:"square"
"""
class CameraClick(BoxLayout):
pass
class DrawBar():
def __init__(self):
self.printer=CameraClick()
def call_back(self,line_heithts = [0.4, 0.5, 0.8, 0.1, 0.3, 0.6, 0.7, 0.4, 0.9, 0.5]):
line_nums_in_layout = len(self.printer.children[0].children[0].children[1].children[2].children)
if line_nums_in_layout > 1:
print(line_nums_in_layout)
for _ in range(line_nums_in_layout - 1):
self.printer.children[0].children[0].children[1].children[2].children.pop(0)
line_color = "{},{},{}, 1"
line_name = "line3"
line1_points = "self.pos[0]-15,self.pos[1]+30+15,self.pos[0]-15,self.pos[1]+self.size[1]*{}+15"
for line_height in line_heithts:
line3 = Builder.load_string(
lines.format(line_name, line_color.format(random.random(), random.random(), random.random()),
line1_points.format(line_height)))
self.printer.children[0].children[0].children[1].children[2].add_widget(line3)
def build(self,line_heithts = [0.4, 0.5, 0.8, 0.1, 0.3, 0.6, 0.7, 0.4, 0.9, 0.5]):
y_labels= ("100", "80","60" , "40", "20","0")
x_labels= ("S","F","D","H","S","A","N","x","y","z")
# 绘制纵坐标
for text in y_labels:
self.printer.children[0].children[0].children[2].children[2].add_widget(Label(text=text))
# 标题
self.printer.children[0].children[0].children[1].children[3].add_widget(Label(text="title",halign="center"))
# 设置横纵坐标轴刻度
# line1
# 绘制坐标轴
self.printer.children[0].children[0].children[1].children[2].add_widget(line1)
self.printer.children[0].children[0].children[1].children[0].add_widget(line2)
# 使用for来控制条带的
self.call_back(line_heithts = [0.4, 0.5, 0.8, 0.1, 0.3, 0.6, 0.7, 0.4, 0.9, 0.5])
# line_nums_in_layout=len(self.printer.children[0].children[0].children[1].children[2].children)
# if line_nums_in_layout >1:
# print(line_nums_in_layout)
# for _ in range(line_nums_in_layout-1):
#
# self.printer.children[0].children[0].children[1].children[2].children.pop(0)
#
#
# line_color="{},{},{}, 1"
# line_name="line3"
# line1_points="self.pos[0]-15,self.pos[1]+30+15,self.pos[0]-15,self.pos[1]+self.size[1]*{}+15"
# line_heithts=[0.4,0.5,0.8,0.1,0.3,0.6,0.7,0.4,0.9,0.5]
# for line_height in line_heithts:
# line3 = Builder.load_string(lines.format(line_name,line_color.format(random.random(),random.random(),random.random()),line1_points.format(line_height)))
# self.printer.children[0].children[0].children[1].children[2].add_widget(line3)
# 绘制横坐标
for text in x_labels:
self.printer.children[0].children[0].children[1].children[1].add_widget(Label(text=text))
return self.printer
if __name__ == '__main__':
runTouchApp(DrawBar().build())
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import random
from kivy.uix.label import Label
from kivy.clock import Clock
Builder.load_string('''
<CaClick>:
orientation: 'vertical'
RelativeLayout:
name:"index_layout"
canvas:
Color:
rgba: 1,1,1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"parent_layout"
rows:1
GridLayout:
name:"left"
cols:1
GridLayout:
name:"left_1"
cols:1
canvas:
Color:
rgba: 0.5, 1, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"left_2"
cols:1
size_hint_y:5
canvas:
Color:
rgba: 1, 0, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"left_3"
cols:1
canvas:
Color:
rgba: 1, 1, 0.6, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"left_4"
cols:1
canvas:
Color:
rgba: 1, 0.9, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"center"
cols:1
size_hint_x:6
GridLayout:
name:"center_1"
rows:1
canvas:
Color:
rgba: 1, 0, 1, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"center_2"
rows:1
size_hint_y:5
GridLayout:
name:"center_3"
rows:1
canvas:
Color:
rgba: 1, 0.5, 0.2, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"center_4"
rows:1
canvas:
Color:
rgba: 1, 0.3, 0.2, 1
Rectangle:
pos:self.pos
size: self.size
GridLayout:
name:"right"
rows:1
''')
line1="""
GridLayout:
name:"line1"
rows:1
canvas:
Color:
rgba: 0.5,0.5,0.3, 1
Line:
points:self.pos[0],self.pos[1]+30,self.pos[0],self.pos[1]+self.size[1]-30
width:5
"""
line2 = """
GridLayout:
name:"line2"
rows:1
canvas:
Color:
rgba: 0.5,0.5,0.3, 1
Line:
points:self.pos[0],self.pos[1]+2*self.size[1]+30,self.pos[0]+self.size[0],self.pos[1]+2*self.size[1]+30
width:5
"""
lines="""
GridLayout:
name:"{}"
rows:1
canvas:
Color:
rgba:{}
Line:
points:{}
width:{}
cap:"square"
"""
class CaClick(BoxLayout):
pass
class DrawBar():
def __init__(self):
self.printer=CaClick()
def call_back(self,line_heights = [0.4, 0.5, 0.8, 0.1, 0.3, 0.6, 0.7, 0.4, 0.9, 0.5]):
line_nums_in_layout = len(self.printer.children[0].children[0].children[1].children[2].children)
if line_nums_in_layout > 1:
print(line_nums_in_layout)
for _ in range(line_nums_in_layout - 1):
self.printer.children[0].children[0].children[1].children[2].children.pop(0)
line_color = "{},{},{}, 1"
line_name = "line3"
line1_points = "self.pos[0]-15,self.pos[1]+30+15,self.pos[0]-15,self.pos[1]+self.size[1]*{}+15"
for line_height in line_heights:
line3 = Builder.load_string(
lines.format(line_name, line_color.format(random.random(), random.random(), random.random()),
line1_points.format(line_height),"self.size[1]//{}".format(2*len(line_heights))))
self.printer.children[0].children[0].children[1].children[2].add_widget(line3)
def build(self,line_heithts = [0.4, 0.5, 0.8, 0.1, 0.3, 0.6, 0.7, 0.4, 0.9, 0.5]):
y_labels= ("100", "80","60" , "40", "20","0")
x_labels= ("S","F","D","H","S","A","N","x","y","z")
# 绘制纵坐标
for text in y_labels:
self.printer.children[0].children[0].children[2].children[2].add_widget(Label(text=text))
# 标题
self.printer.children[0].children[0].children[1].children[3].add_widget(Label(text="title",halign="center"))
# 设置横纵坐标轴刻度
# line1
# 绘制坐标轴
self.printer.children[0].children[0].children[1].children[2].add_widget(Builder.load_string(line1))
self.printer.children[0].children[0].children[1].children[0].add_widget(Builder.load_string(line2))
# 使用for来控制条带的
self.call_back(line_heights = [0.4, 0.5, 0.8, 0.1, 0.3, 0.6, 0.7, 0.4, 0.9, 0.5])
# 绘制横坐标
for text in x_labels:
self.printer.children[0].children[0].children[1].children[1].add_widget(Label(text=text))
return self.printer
class RunNewKivy():
def run_kivy(self,data):
runTouchApp(DrawBar().build(data))
if __name__ == '__main__':
runTouchApp(DrawBar().build())
网友评论