【问题标题】:How to use "if" and "onkeypress" in python turtle?如何在 python turtle 中使用“if”和“onkeypress”?
【发布时间】:2021-06-30 08:19:57
【问题描述】:

'''

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 回调异常

Q:想分离两个动作,绘制多边形,绘制外圆,使用onkeypress。就像,按'4',绘制矩形,然后按'o',绘制它的外圆。如果它在 onkeypress 中不起作用,我希望你能告诉我。

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

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

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

我的解决方案如下。我注意到矩形的外圆和其他圆是不同的。 如何更改代码,例如 if press '4' -> press 'o'(outercircle) -> if not -> get back to the loop -> ? :期望按下相同的按钮“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()

【问题讨论】:

    标签: key-value turtle-graphics getchar python-turtle


    【解决方案1】:

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

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

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


    编辑: 是的,您需要在此处执行类似答案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()
    

    【讨论】:

    • 非常感谢,但是,如果我想使用我按下的 ascii 值,在 Mac os 中我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 2020-08-29
    • 2018-07-28
    • 2019-03-24
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    相关资源
    最近更新 更多