turtle 模塊以麵向對象和麵向過程的方式提供 turtle 圖形基元。由於它使用Tkinter作為基礎圖形,因此需要安裝有Tk支持的Python版本。
turtle .onkey()
此函數用於將樂趣綁定到 key 的key-release事件。為了能夠注冊key-events,TurtleScreen必須具有焦點。
用法:
turtle.onkey(fun, key)
參數:
Arguments | Description |
fun | 沒有參數的函數 |
key | 字符串:鍵(例如“a”)或key-symbol(例如“space”) |
下麵是上述方法的實現和一些示例:
範例1:
Python3
# import package
import turtle
# method for key call
def fxn():
turtle.forward(40)
# set turtle screen size
sc=turtle.Screen()
sc.setup(600,300)
# motion
turtle.forward(40)
# call method on Right key
turtle.onkey(fxn,'Right')
# to listen by the turtle
turtle.listen()
輸出:
範例2:
Python3
# import package
import turtle
# methods with different work
# at different keys
def fxn():
turtle.forward(20)
def fxn1():
turtle.right(90)
def fxn2():
turtle.left(90)
# set screen size
sc=turtle.Screen()
sc.setup(500,300)
# call methods
turtle.onkey(fxn,'space')
turtle.onkey(fxn1,'Right')
turtle.onkey(fxn2,'Left')
# to listen by the turtle
turtle.listen()
輸出:
相關用法
- Python Wand function()用法及代碼示例
- Python hex()用法及代碼示例
- Python dir()用法及代碼示例
- Python now()用法及代碼示例
- Python cmp()用法及代碼示例
- Python ord()用法及代碼示例
- Python str()用法及代碼示例
- Python sum()用法及代碼示例
- Python oct()用法及代碼示例
- Python id()用法及代碼示例
- Python int()用法及代碼示例
- Python tell()用法及代碼示例
- Python map()用法及代碼示例
- Python cmath.sin()用法及代碼示例
注:本文由純淨天空篩選整理自deepanshu_rustagi大神的英文原創作品 turtle.onkey() function in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。