Kivy: 添加一條折線 - 图1
© dotmodus

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Kivy: 添加一條折線 - 图2

Document

Quick Start

By copy the codes from the document, you are supposed being able to show the line on the screen and adjust the alpha, width, etc of the line.

  1. ListProperty([(500, 500),
  2. [300, 300, 500, 300],
  3. [500, 400, 600, 400]])

As you can see above, you can envelope the points by () or [], you can line the points one by one or line them all together if you wish (The examples show down below).

  1. ListProperty([(500, 500),
  2. [300, 300], [500, 300],
  3. [500, 400], [600, 400]])
  4. ```python
  5. or
  6. ```python
  7. ListProperty([500, 500, 300, 300, 500, 300, 500, 400, 600, 400])

The minimal codes you’ve to keep is:

  1. from kivy.app import App
  2. from kivy.uix.floatlayout import FloatLayout
  3. from kivy.lang import Builder
  4. Builder.load_string('''
  5. <LinePlayground>:
  6. # assign a list variate as point so it could be easliy handled in later
  7. point: [0,0, 200, 100]
  8. canvas:
  9. Color:
  10. rgba: .4, .4, 1, 1
  11. Line:
  12. # recall the variate 'point' above
  13. points: self.point
  14. '''
  15. )
  16. class LinePlayground(FloatLayout):
  17. pass
  18. class TestLineApp(App):
  19. def build(self):
  20. return LinePlayground()
  21. if __name__ == '__main__':
  22. TestLineApp().run()

updating the line

If you are not satisfied with drawing a line and want to create an animation, than like do some thing cool.

updating….

  1. from kivy.app import App
  2. from kivy.properties import ListProperty
  3. from kivy.uix.floatlayout import FloatLayout
  4. from kivy.uix.widget import Widget
  5. from kivy.lang import Builder
  6. from kivy.clock import Clock
  7. from kivy.properties import ObjectProperty
  8. ## Remove unnecessary codes from this class
  9. class LinePlayground(FloatLayout):
  10. pass
  11. class Main_app(Widget):
  12. # assign a numeric variate
  13. i = 0
  14. # connect the class in kv file: line -> line_ground -> LinePlayground
  15. line = ObjectProperty(None)
  16. def update(self, dt):
  17. self.i +=1
  18. # update the point
  19. self.line.point = [[0,0,], [self.i,self.i]]
  20. print(self.line.point)
  21. print(self.line)
  22. print("1")
  23. class TestLineApp(App):
  24. def build(self):
  25. Main = Main_app()
  26. # update the result with 60 fps (1/60)
  27. Clock.schedule_interval(Main.update,1/60)
  28. return Main
  29. if __name__ == '__main__':
  30. TestLineApp().run()

testline.kv file:

  1. <LinePlayground>:
  2. # assign a list variate as point so it could be easliy handled in later
  3. point: [0,0, 200, 100]
  4. canvas:
  5. Color:
  6. rgba: .4, .4, 1, 1
  7. Line:
  8. # recall the variate 'point' above
  9. points: self.point
  10. <Main_app>:
  11. # assign the id which could be recall later
  12. line: line_ground
  13. LinePlayground:
  14. id: line_ground
  15. point: [0,10, 100,200]

Kivy: 添加一條折線 - 图3


Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗