python-turtle模块-创建虚拟按钮-实现按钮功能 点击”按钮“后触发对应功能!

python-turtle模块-创建虚拟按钮-实现按钮功能 点击”按钮“后触发对应功能

使用turtle模块的时候,有时需要创建“按钮”,但是据我所知,turtle似乎还没有按钮的创建方法,所以,我想到了利用鼠标点击位置来实现一个虚拟的 “按钮”!上代码:

先导入

import turtle as t#为了让大家阅读方便,没有from turtle import *

一些基本的设置

t.color('#FF0033');t.pu();t.ht();t.speed(0);t.title('模拟按钮')#比较简单,初学者也能看懂,所以用了分号

开始画按钮

def 画退出按钮():
	t.goto(10,10);t.pd();t.goto(10,40);t.goto(40,40)
	t.goto(40,10);t.goto(10,10);t.pu();t.goto(26,16)
    t.write('退出',align="center", font=("微软雅黑",11,"bold"))
画退出按钮()

虚拟按钮被点击

def coordinate(x,y):
    print('鼠标点击:X坐标:{}  Y坐标:{}'.format(x,y));
    if x>10 and y>10 and x<40 and y<40:t.bye();print('事件:退出')
t.onscreenclick(coordinate)

结束

t.done()

运行

图片:在这里插入图片描述

代码:

import turtle as t
t.color('#FF0033');t.pu();t.ht();t.speed(0);t.title('模拟按钮')
def 画退出按钮():
    t.goto(10,10);t.pd();t.goto(10,40);t.goto(40,40);t.goto(40,10);t.goto(10,10);t.pu();t.goto(26,16)
    t.write('退出',align="center", font=("微软雅黑",11,"bold"))
画退出按钮()
def coordinate(x,y):
    print('鼠标点击:X坐标:{}  Y坐标:{}'.format(x,y));
    if x>10 and y>10 and x<40 and y<40:t.bye();print('事件:退出')
t.onscreenclick(coordinate)
t.done()


代码图片:
在这里插入图片描述
这个"退出"按钮会了,其他还不会吗?以此类推!
来都来了,给个👍吧!

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值