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评论83阅读模式
英文:

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

问题

  1. from tkinter import *
  2. c = Canvas(width=800, height=600, bg="White")
  3. rectangles = []
  4. for x in range(50, 500, 40):
  5. rectangles.append(c.create_rectangle(x, 50, x + 20, 150, outline="Blue",
  6. width=2, fill="Gray", activefill="Red"))
  7. for rect in rectangles:
  8. c.tag_bind(rect, "<Enter>", lambda event, rect=rect: c.itemconfig(rect, fill="Red"))
  9. c.tag_bind(rect, "<Leave>", lambda event, rect=rect: c.itemconfig(rect, fill="Gray"))
  10. c.pack()
  11. 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:

  1. from tkinter import *
  2. c=Canvas(width=800, height=600, bg=&quot;White&quot;)
  3. c.create_rectangle(50,50,70,150,outline=&quot;Blue&quot;,
  4. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  5. c.create_rectangle(90,50,110,150,outline=&quot;Blue&quot;,
  6. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  7. c.create_rectangle(130,50,150,150,outline=&quot;Blue&quot;,
  8. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  9. c.create_rectangle(170,50,190,150,outline=&quot;Blue&quot;,
  10. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  11. c.create_rectangle(210,50,230,150,outline=&quot;Blue&quot;,
  12. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  13. c.create_rectangle(250,50,270,150,outline=&quot;Blue&quot;,
  14. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  15. c.create_rectangle(290,50,310,150,outline=&quot;Blue&quot;,
  16. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  17. c.create_rectangle(330,50,350,150,outline=&quot;Blue&quot;,
  18. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  19. c.create_rectangle(370,50,390,150,outline=&quot;Blue&quot;,
  20. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  21. c.create_rectangle(410,50,430,150,outline=&quot;Blue&quot;,
  22. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  23. c.create_rectangle(450,50,470,150,outline=&quot;Blue&quot;,
  24. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  25. c.create_rectangle(490,50,510,150,outline=&quot;Blue&quot;,
  26. width=2,fill=&quot;Gray&quot;,activefill=&quot;Red&quot;)
  27. c.pack()
  28. 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循环进行的更改:

  1. from tkinter import *
  2. c = Canvas(width=800, height=600, bg="White")
  3. cords = [50, 50, 70, 150]
  4. while cords[0] <= 490 and cords[2] <= 510:
  5. c.create_rectangle(cords, outline="Blue", width=2, fill="Gray", activefill="Red")
  6. cords[0] += 40
  7. cords[2] += 40
  8. c.pack()
  9. mainloop()
英文:

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

  1. from tkinter import *
  2. c = Canvas(width=800, height=600, bg=&quot;White&quot;)
  3. cords = [50, 50, 70, 150]
  4. while cords[0] &lt;= 490 and cords[2] &lt;= 510:
  5. c.create_rectangle(cords, outline=&quot;Blue&quot;, width=2, fill=&quot;Gray&quot;, activefill=&quot;Red&quot;)
  6. cords[0] += 40
  7. cords[2] += 40
  8. c.pack()
  9. mainloop()

答案2

得分: 1

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

  1. c = Canvas(width=800, height=600, bg="White")
  2. # 'range(12)'返回一个包含12个数字的序列,从0到11
  3. # 你可以使用for循环迭代这个序列
  4. for i in range(12):
  5. # 计算偏移量
  6. x1 = (40 * (i + 1) + 10)
  7. x2 = (40 * (i + 1) + 30)
  8. # 创建坐标元组
  9. coords = (x1, 50, x2, 150)
  10. # 使用适当的坐标创建一个矩形
  11. 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

  1. c = Canvas(width=800, height=600, bg=&quot;White&quot;)
  2. # &#39;range(12)&#39; returns a sequence of 12 numbers, from 0 to 11
  3. # you can iterate over that sequence with a for loop
  4. for i in range(12):
  5. # calculate offsets
  6. x1 = (40 * (i + 1) + 10)
  7. x2 = (40 * (i + 1) + 30)
  8. # create a tuple of coordinates
  9. coords = (x1, 50, x2, 150)
  10. # create a rectangle with the appropriate coordinates
  11. 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:

确定