调用事件函数并使用 .bind 使我的画布矩形在光标滚动后显示为灰色

编程入门 行业动态 更新时间:2024-10-24 14:16:37
本文介绍了调用事件函数并使用 .bind 使我的画布矩形在光标滚动后显示为灰色 - Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我需要在光标滑过矩形后使矩形显示为灰色.但我不确定如何做到这一点.我知道这与Enter"功能有关,并且可能将颜色更改为灰色.

I need the rectangles to appear grey after the cursor has been rolled over it. But I am not sure how exactly to do that. I know it has to do with the "Enter" function and maybe changing the colour to grey.

如果有人可以帮助我处理代码位,那就太好了.另外我假设如果我想在一个新级别重复这个过程,我会使用循环计数器吗?谢谢.这是我的画布代码:

If anyone can help me with the code bit that would be great. Also I assume that if I want to repeat this process for a new level would I use a loop counter? Thanks. Here is my code for the Canvas:

canvas = Canvas(PageOne, width=600, height=800)
canvas.pack()

#                                  x1  y1  x2  y2
greenBox = canvas.create_rectangle(40, 60, 90, 110, fill="green")
RedBox = canvas.create_rectangle(110, 60, 160, 110, fill="red")
BlueBox = canvas.create_rectangle(180, 60, 230, 110, fill="blue", activefill="grey")

推荐答案

在以下示例中,当您的鼠标光标第一次悬停在一个矩形上时,其颜色变为灰色:

In the following example, when your mouse cursor hovers over a rectangle for the first time, its color turns to grey:

import tkinter as tk

def turngrey(e):
    canvas.itemconfig('current', fill='grey')

root = tk.Tk()
canvas = tk.Canvas(root, width=600, height=800)
canvas.pack()

greenBox = canvas.create_rectangle(40, 60, 90, 110, fill="green", tags=('turntogrey'))
RedBox = canvas.create_rectangle(110, 60, 160, 110, fill="red", tags=('turntogrey'))
BlueBox = canvas.create_rectangle(180, 60, 230, 110, fill="blue", tags=('turntogrey'))

canvas.tag_bind('turntogrey', '<Enter>', turngrey)

root.mainloop()

这篇关于调用事件函数并使用 .bind 使我的画布矩形在光标滚动后显示为灰色 - Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:31:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390771.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:画布   矩形   光标   使我   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!