How do I compress repeating tkinter code into a loop so it displays rectangles without having to write out each individual rectangle's coordinates?

huangapple go评论67阅读模式
英文:

How do I compress repeating tkinter code into a loop so it displays rectangles without having to write out each individual rectangle's coordinates?

问题

from tkinter import *

c = Canvas(width=800, height=600, bg="White")
rectangles = []

for x in range(50, 500, 40):
    rectangles.append(c.create_rectangle(x, 50, x + 20, 150, outline="Blue",
                   width=2, fill="Gray", activefill="Red"))

for rect in rectangles:
    c.tag_bind(rect, "<Enter>", lambda event, rect=rect: c.itemconfig(rect, fill="Red"))
    c.tag_bind(rect, "<Leave>", lambda event, rect=rect: c.itemconfig(rect, fill="Gray"))

c.pack()
mainloop()

这段代码将原来重复的矩形绘制过程用循环简化了,同时添加了鼠标悬停事件,以改变矩形的颜色。

英文:

I have some code that repeats that I would like to compress into a loop somehow. The code is meant to show a series of rectangles that change color when your mouse hovers over them, and because I have numerous rectangles, all of the code repeating is the same except for 2 coordinates for the location of my rectangles. Therefore, I want to somehow compress the following code into a loop:

from tkinter import *

c=Canvas(width=800, height=600, bg=&quot;White&quot;)

c.create_rectangle(50,50,70,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.create_rectangle(90,50,110,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.create_rectangle(130,50,150,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.create_rectangle(170,50,190,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.create_rectangle(210,50,230,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.create_rectangle(250,50,270,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.create_rectangle(290,50,310,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.create_rectangle(330,50,350,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.create_rectangle(370,50,390,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.create_rectangle(410,50,430,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.create_rectangle(450,50,470,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.create_rectangle(490,50,510,150,outline=&quot;Blue&quot;,
                   width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
c.pack()
mainloop()

My first idea to approaching this was setting the original rectangle dimensions to (x, 50, y, 150), setting x=50 and y=70, and then somehow incorporating that while x<=490 and y<=510, x=x+40. However, I don't know how to incorporate all of this into a loop.

答案1

得分: 3

这是您要使用while循环进行的更改:

from tkinter import *

c = Canvas(width=800, height=600, bg="White")
cords = [50, 50, 70, 150]

while cords[0] <= 490 and cords[2] <= 510:
    c.create_rectangle(cords, outline="Blue", width=2, fill="Gray", activefill="Red")
    cords[0] += 40
    cords[2] += 40

c.pack()
mainloop()
英文:

This is the alteration you are looking for using a while loop:

from tkinter import *

c = Canvas(width=800, height=600, bg=&quot;White&quot;)
cords = [50, 50, 70, 150]

while cords[0] &lt;= 490 and cords[2] &lt;= 510:
    c.create_rectangle(cords, outline=&quot;Blue&quot;, width=2, fill=&quot;Gray&quot;, activefill=&quot;Red&quot;)
    cords[0] += 40
    cords[2] += 40

c.pack()
mainloop()

答案2

得分: 1

你可以使用for循环来完成这个操作,使用一些数学来更新每次迭代的偏移量,因为它们总是以相同的数量更改。

c = Canvas(width=800, height=600, bg="White")

# 'range(12)'返回一个包含12个数字的序列,从0到11
# 你可以使用for循环迭代这个序列
for i in range(12):
    # 计算偏移量
    x1 = (40 * (i + 1) + 10)
    x2 = (40 * (i + 1) + 30)
    # 创建坐标元组
    coords = (x1, 50, x2, 150)
    # 使用适当的坐标创建一个矩形
    c.create_rectangle(coords, outline="Blue", width=2, fill="Gray", activefill="Red")

注意,我在i的值上加了1,因为range可迭代对象从0开始。

英文:

You can do this with a for loop, using some math to update the offsets each iteration since they always change by the same amount

c = Canvas(width=800, height=600, bg=&quot;White&quot;)

# &#39;range(12)&#39; returns a sequence of 12 numbers, from 0 to 11
# you can iterate over that sequence with a for loop
for i in range(12):
    # calculate offsets
    x1 = (40 * (i + 1) + 10)
    x2 = (40 * (i + 1) + 30)
    # create a tuple of coordinates
    coords = (x1, 50, x2, 150)
    # create a rectangle with the appropriate coordinates
    c.create_rectangle(coords, outline=&quot;Blue&quot;, width=2, fill=&quot;Gray&quot;, activefill=&quot;Red&quot;)

Note that I'm adding 1 to the value of i because range iterables start at 0

huangapple
  • 本文由 发表于 2023年2月18日 02:28:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定