Casa > H > How Do We Create Graphics In Python?

How do we create graphics in Python?

You use the turtle module to create graphics in Python. I’ll add some of the simple scripts I wrote when I was learning Python.

  • This script creates a simple never stopping concentric polygon.
  1. import turtle 
  2. def draw(t, length, n): 
  3. if n == 0: 
  4. return 
  5. angle = 60 
  6.  
  7.  
  8.  
  9. t.fd(length*n) 
  10. t.lt(angle) 
  11. draw(t, length, n-1) 
  12. t.rt(2*angle) 
  13. draw(t, length, n-1) 
  14. t.lt(angle) 
  15. t.bk(length*n) 
  16.  
  17. bob= turtle.Turtle() 
  18. print (draw(bob, 1, 1/3)) 
  19. turtle.mainloop() 
  • This script creates a three coloured cirlce. :p
  1. import turtle 
  2.  
  3. def shape(): 
  4.  
  5. window = turtle.Screen() 
  6. window.bgcolor("white") 
  7.  
  8.  
  9.  
  10. asdf = turtle.Turtle() 
  11. asdf.shape("turtle") 
  12.  
  13. asdf.speed(500) 
  14. for j in range(120): 
  15. asdf.color("blue") 
  16. for i in range(4): 
  17. asdf.forward(100) 
  18. asdf.right(90) 
  19. asdf.right(1) 
  20. for j in range(120): 
  21. asdf.color("green") 
  22. for i in range(4): 
  23. asdf.forward(100) 
  24. asdf.right(90) 
  25. asdf.right(1) 
  26. for j in range(120): 
  27. asdf.color("yellow") 
  28. for i in range(4): 
  29. asdf.forward(100) 
  30. asdf.right(90) 
  31. asdf.right(1) 
  32.  
  33.  
  34. window.exitonclick() 
  35. shape() 

You can run these scripts using python3 and if it increases your curiosity, you can learn about it more from various resources.

De Amal Robbert

Qual telefone é melhor, Mi4i ou Meizu M2 Note? :: Tenho 45 anos de idade. Já é tarde para eu começar a construir o meu corpo?