turtle 模块以面向对象和面向过程的方式提供 turtle 图形基元。由于它使用Tkinter作为基础图形,因此需要安装有Tk支持的Python版本。
turtle .onscreenclick()
此函数用于将乐趣绑定到画布上的mouse-click事件。
用法:
turtle.onscreenclick(fun, btn=1, add=None)
参数:
参数 | 描述 |
fun | 一个具有两个参数的函数,即画布上单击点的坐标。 |
btn | mouse-button的编号默认为1(鼠标左键) |
add | 对或错。如果为True,将添加新的绑定,否则它将替换以前的绑定 |
下面是上述方法的实现示例:
Python3
# import packages
import turtle
import random
# global colors
col = ['red', 'yellow', 'green', 'blue',
'white', 'black', 'orange', 'pink']
# method to call on screen click
def fxn(x, y):
global col
ind = random.randint(0, 7)
# set screen color randomly
sc.bgcolor(col[ind])
# set screen
sc = turtle.Screen()
sc.setup(400, 300)
# call method on screen click
turtle.onscreenclick(fxn)
输出:
在这里我们可以发现,只要用户单击屏幕上的(箭头上的yellow-colored点),它就会随机更改龟图形窗口的背景颜色。
相关用法
- Python Wand function()用法及代码示例
- Python id()用法及代码示例
- Python int()用法及代码示例
- Python tell()用法及代码示例
- Python dir()用法及代码示例
- Python map()用法及代码示例
- Python oct()用法及代码示例
- Python cmp()用法及代码示例
- Python ord()用法及代码示例
- Python str()用法及代码示例
- Python sum()用法及代码示例
- Python hex()用法及代码示例
- Python now()用法及代码示例
- Python turtle.dot()用法及代码示例
- Python reversed()用法及代码示例
注:本文由纯净天空筛选整理自deepanshu_rustagi大神的英文原创作品 turtle.onscreenclick() function in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。