Reputation: 25
'''
from sys import exit
from turtle import *
from math import sqrt, cos, radians
def tria():
seth(180)
fd(radius*sqrt(3))
lt(120)
fd(radius *sqrt(3))
lt(120)
fd(radius * sqrt(3))
seth(120)
circle(radius)
seth(0)
def rect():
seth(0)
fd(radius * sqrt(2))
rt(90)
fd(radius * sqrt(2))
rt(90)
fd(radius * sqrt(2))
rt(90)
fd(radius * sqrt(2))
seth(0)
def penta():
seth(-36)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
seth(180)
def hexa():
seth(-30)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
seth(180)
radius = float(input("Write the radius "))
screen = getscreen()
shape('turtle')
speed(5)
screen.onkeypress(tria,"3")
screen.onkeypress(reset,"r")
screen.onkeypress(exit,"q")
screen.onkeypress(exit,"q")
if (screen.onkeypress(None,'4')):
rect()
if (screen.onkeypress(None,'o')):
seth(120)
circle(radius)
seth(0)
screen.listen()
screen.mainloop()
'''
Error : jinhojeon@jinui-MacBookAir jinhojeon % python polygons2.py Write the radius 100 2021-06-30 18:10:06.982 python[10480:325389] TSM AdjustCapsLockLEDForKeyTransitionHandling - _ISSetPhysicalKeyboardCapsLockLED Inhibit Exception in Tkinter callback
Q : Want to separate two actions, drawing polygons, drawing the outer circle by using onkeypress. Like, pressing '4', drawing rectangular, and then pressing 'o', drawing its outer circle. If it's not working in onkeypress, I hope you would indicate that to me.
Problem : Don't know how to call press-key's str or ascii in python, in Mac os. I don't know either that it is working even if I could "if ~~ == 'o': onkeypress(outer_rect,'o')
.
As I know, the function onkeypress is meant to do "fun" when the appropriate key is read. I googled it before, but, "msvcrt" doesn't work in Mac. Anybody knows how to get press-key as str or ascii in python with mac or how to separate them well. I'd really appreciate it if you could share with me your knowledge.
If my words are vague, I am really thankful for you to point out which are vague.
My solution is below. I noticed that outer circle of rectangular and the others are different. How to change the code like if press '4' -> press 'o'(outercircle) -> if not -> get back to the loop -> ? : expect that press same button 'o', but different output
from sys import exit
from turtle import *
from math import sqrt, cos, radians
def tria():
speed(6)
seth(180)
fd(radius*sqrt(3))
lt(120)
fd(radius *sqrt(3))
lt(120)
fd(radius * sqrt(3))
seth(120)
def rect():
speed(6)
seth(0)
fd(radius * sqrt(2))
rt(90)
fd(radius * sqrt(2))
rt(90)
fd(radius * sqrt(2))
rt(90)
fd(radius * sqrt(2))
seth(0)
def penta():
speed(6)
seth(-36)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
seth(180)
def hexa():
speed(6)
seth(-30)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
seth(180)
def one():
speed(6)
seth(225)
circle(radius)
seth(0)
def outercircle():
speed(6)
circle(radius)
seth(0)
radius = float(input("Write the radius "))
screen = getscreen()
shape('turtle')
speed(6)
screen.listen()
screen.onkeypress(reset,"r")
screen.onkeypress(exit,"q")
screen.onkeypress(tria,"3")
screen.onkeypress(rect,'4')
screen.onkeypress(penta,'5')
screen.onkeypress(hexa,'6')
screen.onkeypress(one,'o')
screen.onkeypress(outercircle,'C')
screen.mainloop()
Upvotes: 0
Views: 1194
Reputation: 2145
That was hard to follow. Hope you got the keyboard thing straightened out.
Thought it was an unbound turtle, then I realized you used from turtle import *
which, I guess allows you to type in commands without declaring a turtle instance. Then I thought you were saying that it needed screen.listen()
but that was typed in above where I expected it. Thought that'd be under your keybindings. Guess it runs fine either way.
So your real question is about how to keep track of the current drawstyle, then make a circle specific for that. Just set a global, and use an if-then.
Edit: Yea, you'd need to do something like the answer here, Python Turtle screen.onkey() to print out keysymbols, and figure out which is required. Then type that into your script. Combos would look like this, Tkinter keybinding for Control-Shift-Tab
#! /usr/bin/env python3
from sys import exit
from turtle import *
from math import sqrt, cos, radians
current = None
def tria():
global current ; current = 'tria'
speed(6)
seth(180)
fd(radius*sqrt(3))
lt(120)
fd(radius *sqrt(3))
lt(120)
fd(radius * sqrt(3))
seth(120)
def rect():
global current ; current = 'rect'
speed(6)
seth(0)
fd(radius * sqrt(2))
rt(90)
fd(radius * sqrt(2))
rt(90)
fd(radius * sqrt(2))
rt(90)
fd(radius * sqrt(2))
seth(0)
def penta():
global current ; current = 'penta'
speed(6)
seth(-36)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
rt(72)
fd(cos(radians(54))*radius*2)
seth(180)
def hexa():
global current ; current = 'hexa'
speed(6)
seth(-30)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
rt(60)
fd(radius)
seth(180)
def one():
global current ; current = 'one'
speed(6)
seth(225)
circle(radius)
seth(0)
def outercircle():
if current == 'tria':
seth(120)
speed(6)
circle(radius)
seth(0)
if current == 'rect':
seth(224)
speed(6)
circle(radius *1.01)
seth(0)
else:
speed(6)
circle(radius)
seth(0)
def test( event ): ## print keysymbols, so you can see which to use
print('test event:', event)
print('test keysym:', event.keysym)
print('test state:', event.state)
print('test Ctrl :', bool(event.state & 4))
print('test Shift:', bool(event.state & 1))
print('test Alt :', bool(event.state & 8))
print('---')
radius = float(input("Write the radius "))
screen = getscreen()
canvas = screen.getcanvas() ## get the raw tkinter canvas
shape('turtle')
speed(6)
screen.listen()
screen.onkeypress(reset,"r")
screen.onkeypress(exit,"q")
screen.onkeypress(tria,"3")
screen.onkeypress(rect,'4')
screen.onkeypress(penta,'5')
screen.onkeypress(hexa,'6')
screen.onkeypress(one,'o')
screen.onkeypress(outercircle,'c')
canvas.bind( '<KeyPress>', test ) ## bind any unbound keys to the test() function
screen.mainloop()
Upvotes: 1