2018-10-07 05:03:18 +00:00
|
|
|
#!/usr/bin/env python
|
2019-10-23 20:10:03 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-10-07 05:03:18 +00:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
layout = [[sg.Text('My layout')],
|
|
|
|
[sg.Canvas(size=(800, 800), key='-canvas-')],
|
|
|
|
[sg.Button('F'), sg.Button('B'), sg.Button('L'), sg.Button('R')],
|
2018-10-29 00:01:03 +00:00
|
|
|
[sg.Button('Spiral'), sg.Button('Inside Out'), sg.Button('Circles')]]
|
2018-10-07 05:03:18 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('My new window', layout, finalize=True)
|
2018-10-07 05:03:18 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
canvas = window['-canvas-'].TKCanvas
|
2018-10-07 05:03:18 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
a_turtle = turtle.RawTurtle(canvas)
|
|
|
|
a_turtle.pencolor("#ff0000") # Red
|
|
|
|
a_turtle.penup()
|
|
|
|
a_turtle.pendown()
|
2018-10-07 05:03:18 +00:00
|
|
|
|
|
|
|
while True: # Event Loop
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read()
|
2018-10-15 20:07:23 +00:00
|
|
|
if event is None:
|
2018-10-07 05:03:18 +00:00
|
|
|
break
|
|
|
|
|
2018-10-15 20:07:23 +00:00
|
|
|
if event == 'F':
|
2019-10-23 20:10:03 +00:00
|
|
|
a_turtle.forward(100)
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'B':
|
2019-10-23 20:10:03 +00:00
|
|
|
a_turtle.back(100)
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'L':
|
2019-10-23 20:10:03 +00:00
|
|
|
a_turtle.left(90)
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'R':
|
2019-10-23 20:10:03 +00:00
|
|
|
a_turtle.right(90)
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'Spiral':
|
|
|
|
canvas.config(bg='light green')
|
2019-10-23 20:10:03 +00:00
|
|
|
a_turtle.color("blue")
|
|
|
|
|
2018-10-15 20:07:23 +00:00
|
|
|
def sqrfunc(size):
|
|
|
|
for i in range(4):
|
2019-10-23 20:10:03 +00:00
|
|
|
a_turtle.fd(size)
|
|
|
|
a_turtle.left(90)
|
2018-10-15 20:07:23 +00:00
|
|
|
size = size - 5
|
|
|
|
sqrfunc(146)
|
|
|
|
sqrfunc(126)
|
|
|
|
sqrfunc(106)
|
|
|
|
sqrfunc(86)
|
|
|
|
sqrfunc(66)
|
|
|
|
sqrfunc(46)
|
|
|
|
sqrfunc(26)
|
|
|
|
elif event == 'Inside Out':
|
2019-10-23 20:10:03 +00:00
|
|
|
canvas.config(bg="light green")
|
|
|
|
a_turtle.color("blue")
|
|
|
|
|
2018-10-15 20:07:23 +00:00
|
|
|
def sqrfunc(size):
|
|
|
|
for i in range(4):
|
2019-10-23 20:10:03 +00:00
|
|
|
a_turtle.fd(size)
|
|
|
|
a_turtle.left(90)
|
2018-10-15 20:07:23 +00:00
|
|
|
size = size + 5
|
|
|
|
sqrfunc(6)
|
|
|
|
sqrfunc(26)
|
|
|
|
sqrfunc(46)
|
|
|
|
sqrfunc(66)
|
|
|
|
sqrfunc(86)
|
|
|
|
sqrfunc(106)
|
|
|
|
sqrfunc(126)
|
|
|
|
sqrfunc(146)
|
|
|
|
elif event == 'Circles':
|
2019-10-23 20:10:03 +00:00
|
|
|
a_turtle.speed(0)
|
2018-10-15 20:07:23 +00:00
|
|
|
for i in range(400):
|
2019-10-23 20:10:03 +00:00
|
|
|
a_turtle.circle(2 * i*.25)
|
|
|
|
a_turtle.circle(-2 * i*.25)
|
|
|
|
a_turtle.left(i)
|
|
|
|
window.close()
|