-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathDemo_Turtle.py
More file actions
85 lines (72 loc) · 2.24 KB
/
Demo_Turtle.py
File metadata and controls
85 lines (72 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
import PySimpleGUI as sg
import turtle
"""
Demo showing how to integrate drawing on a Canvas using Turtle with PySimpleGUI
The patern to follow:
Create Window & Finalize
Get the tkinter Canvas from the Canvas element
Draw on the tkinter Canvas using turtle commands.
Results are shown on the canvas immiedately after button press / drawing command
Copyright 2018-2026 PySimpleGUI. All rights reserved.
"""
layout = [[sg.Text('My layout')],
[sg.Canvas(size=(800, 800), key='-canvas-')],
[sg.Button('F'), sg.Button('B'), sg.Button('L'), sg.Button('R')],
[sg.Button('Spiral'), sg.Button('Inside Out'), sg.Button('Circles')]]
window = sg.Window('My new window', layout, finalize=True)
canvas = window['-canvas-'].TKCanvas
a_turtle = turtle.RawTurtle(canvas)
a_turtle.pencolor("#ff0000") # Red
a_turtle.penup()
a_turtle.pendown()
while True: # Event Loop
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'F':
a_turtle.forward(100)
elif event == 'B':
a_turtle.back(100)
elif event == 'L':
a_turtle.left(90)
elif event == 'R':
a_turtle.right(90)
elif event == 'Spiral':
canvas.config(bg='light green')
a_turtle.color("blue")
def sqrfunc(size):
for i in range(4):
a_turtle.fd(size)
a_turtle.left(90)
size = size - 5
sqrfunc(146)
sqrfunc(126)
sqrfunc(106)
sqrfunc(86)
sqrfunc(66)
sqrfunc(46)
sqrfunc(26)
elif event == 'Inside Out':
canvas.config(bg="light green")
a_turtle.color("blue")
def sqrfunc(size):
for i in range(4):
a_turtle.fd(size)
a_turtle.left(90)
size = size + 5
sqrfunc(6)
sqrfunc(26)
sqrfunc(46)
sqrfunc(66)
sqrfunc(86)
sqrfunc(106)
sqrfunc(126)
sqrfunc(146)
elif event == 'Circles':
a_turtle.speed(0)
for i in range(400):
a_turtle.circle(2 * i*.25)
a_turtle.circle(-2 * i*.25)
a_turtle.left(i)
window.close()