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