尽管如上所述,您可能更喜欢使用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
中绘制它们(如指出通过下面布赖恩·奥克利),你可以使用Unicode字符的箭头,这将是一个可怕的很简单。
也许我想得简单,但你有没有想过生成HTML页面,并使用CSS来安排箱子?当窗口尺寸改变时会允许动态布局吗? – Arminius
这实际上有很大的意义......我是新手,对于像我这样的问题有很多答案,我只是迷失在他们身上。任何建议关于这个主题的教程?把情节/酒吧放在同一页面你会说什么是最好的? – user3755529
作为Aminius建议,您可以使用[HTML的子集和CSS(https://doc.qt.io/qt-5/richtext-html-subset.html)格式的文本,并把它放在一个'QLabel' 。布局文本的最简单方法是使用html表格。 – ekhumoro