虽然您可能最好使用上面提到的 HTML 和 CSS 执行此操作,但使用 Python 执行此操作并不难,只需使用 tkinter 即可实现。请参阅下面的代码以了解其工作原理:
from tkinter import *
root = Tk()
frame1 = []
frame2 = []
c = 0
numberofboxes = 8 #change this to increase the number of boxes
for i in range(numberofboxes):
if i % 4 == 0: #checks if the current box is the fourth in row
c = c + 1 #if the current box is the forth in the row then this runs and increases a counter which we later use to determine the row
if len(frame1) != c: #checks if the number of rows currently existing matches the number there should be
frame1.append(Frame(root)) #if the numbers don't match this runs and creates a new frame which acts as another row
frame1[c-1].pack(expand="True", fill="both") #packs the new row
frame2.append(Frame(frame1[c-1], bg="green")) #this is where the boxes are created
frame2[i].pack(ipadx="50", ipady="50", side="left", padx="10", pady="10", expand="True", fill="both") #this is where the boxes are placed on the screen
for i in range(len(frame2)): #this for loop places the items inside each box, all of this can be replaced with whatever is needed
Label(frame2[i], text="CO"+str(i), bg="green", fg="white").pack(side="top", anchor="w")
Label(frame2[i], text="12165.1"+str(i), bg="green", fg="white").pack(side="top", anchor="w")
Label(frame2[i], text="+60.7"+str(i), bg="green", fg="white").pack(side="bottom", anchor="e")
Label(frame2[i], text="+1.2"+str(i)+"%", bg="green", fg="white").pack(side="bottom", anchor="e")
root.mainloop()
因此,本质上,我们为每一行创建一个frame,每个框都是一个frame,其中包含元素,并适合每行的“行frame”4。
您应该在此脚本期间仔细查看.pack() 的所有选项,以及它们对于实现所需布局和结果所必需的。
对于您的三角形,您很可能需要导入图像或在正确定位的canvas 内绘制它们,或者(正如下面的 Bryan Oakley 所指出的)您可以使用 unicode 字符作为箭头,这将是非常简单。