如何在python乌龟中使用"if"和"onkeypress"?

How to use "if" and "onkeypress" in python turtle?

'''

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 python[10480:325389] TSM AdjustCapsLockLEDForKeyTransitionHandling - _ISSetPhysicalKeyboardCapsLockLED 抑制 Tkinter 回调异常

问:想分开两个动作,画多边形,画外圈用onkeypress。例如,按“4”,绘制矩形,然后按 'o',绘制外圆。如果它在onkeypress中不起作用,我希望你向我指出。

问题:不知道如何在 python、Mac os 中调用按键的 str 或 ascii。我也不知道它是否有效,即使我可以"if ~~ == 'o': onkeypress(outer_rect,'o')

据我所知,onkeypress 函数的目的是在读取适当的键时“有趣”。我之前用谷歌搜索过,但是,“msvcrt”在 Mac 中不起作用。任何人都知道如何在 python 和 mac 中将按键作为 str 或 ascii 获取,或者如何将它们很好地分开。如果您能与我分享您的知识,我将不胜感激。

如果我的话含糊不清,我真的很感谢你指出哪些含糊不清。

我的解决方案如下。我注意到矩形的外圈和其他的不一样。 如何更改代码,如按“4” -> 按 'o'(outercircle) -> 如果不按 -> 返回循环 -> ? : 期望按下相同的按钮 '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()

这很难理解。希望你把键盘的事情理顺了。

以为它是一只未绑定的海龟,后来我意识到你使用了 from turtle import *,我猜它允许你在不声明海龟实例的情况下输入命令。然后我以为你是说它需要 screen.listen() 但那是在我预期的 以上 中输入的。以为那会在您的键绑定下。猜想无论哪种方式它都运行良好。

所以你真正的问题是关于如何跟踪当前的绘图样式,然后为此创建一个特定的圆圈。只需设置一个全局,然后使用 if-then。


编辑: 是的,您需要像这里的答案那样做一些事情,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()