如何解决如何在python乌龟中使用“if”和“onkeypress”?
'''
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 % pythonpolygons2.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 获取,或者如何将它们很好地分开。如果您能与我分享您的知识,我将不胜感激。
如果我的话含糊不清,我真的很感谢您指出哪些含糊不清。
我的解决方案如下。我注意到矩形的外圆和其他的不同。 如何更改代码,例如按“4”-> 按“o”(外圆)-> 如果不是-> 返回循环-> ? : 期望按下相同的按钮“o”,但输出不同
from sys import exit
from turtle import *
from math import sqrt,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,"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() 以打印出键符号,并确定需要哪个。然后将其输入到您的脚本中。组合看起来像这样,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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。