“”“
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()“”“
错误: jinhojeon@jinui-MacBookAir jinhojeon % python polygons2.py写半径100 2021-06-30 18:10:06.982 python10480:325389 TSM AdjustCapsLockLEDForKeyTransitionHandling - _ISSetPhysicalKeyboardCapsLockLED抑制异常
问:要分开两个动作,画多边形,用按键画外圆。例如,按'4',画长方形,然后按'o',画它的外圆。如果它不起作用的话,我希望你能告诉我。
问题:不知道如何在python和Mac os中调用press-key的str或ascii。我也不知道它是否有效,即使我可以"if ~~ == 'o': onkeypress(outer_rect,'o')。
如我所知,当读取适当的键时,onkeypress上的函数就意味着“有趣”。我以前在谷歌上搜索过,但是,"msvcrt“在Mac上不起作用。任何人都知道如何在使用mac的python中获得str或ascii的按键,或者如何将它们很好地分开。如果你能和我分享你的知识,我会非常感激的。
如果我的话含糊不清,我真的很感谢你指出哪些是模糊的。
我的解决方案如下。我注意到长方形的外圆和其他的是不同的。如何更改代码,如按'4‘->按’o‘(离开循环) ->,如果不按->返回循环->?:期望按相同的按钮'o',但输出不同
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()发布于 2021-06-30 11:33:25
很难理解。希望你把键盘问题弄清楚了。
我以为它是一只未绑定的海龟,然后我意识到你使用了from turtle import *,我想它允许你输入命令,而不需要声明海龟实例。然后,我以为您在说它需要screen.listen(),但它是在而不是中输入的,而我正是在那里预料到的。我以为那会在你的钥匙绑定下。不管怎么说都行。
所以,您真正的问题是如何跟踪当前的绘图风格,然后为其设置一个圆圈。只需设置一个全局,并使用如果-然后。
编辑: Yea,您需要做一些类似于这里的答案,Python Turtle screen.onkey()来打印出关键符号,并确定哪一个是必需的。然后把它输入你的脚本中。组合体应该是这样的,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()https://stackoverflow.com/questions/68190987
复制相似问题