问题描述
限时送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:关键词]
发布评论