2017-08-14 71 views
1

我期待写一个程序,这样的形象在输出盒一堆(约30)文本布局:最佳模块在Python

market watchlist

我一直在研究了几个星期,我想以图形化的方式布局文本,我可以在PyQT中使用QTableWidget,但是现在我意识到要完成这样一个简单的任务是非常困难的,必须有一个更快的方法。所以我现在想要传递给Tkinter,或者只是使用像PyCairo这样的绘图模块来绘制信息,然后才能将每个图像放置在PyQT界面中。对绘图模块中的所有定位进行排序比学习如何在PyQT中执行相同的操作要快得多。

但我觉得我失去了一些东西,我会想到一个更容易的任务在一个不错的方式来布局一串数字在重复格式。

有些箱子还需要一些图形内容有酒吧和图表我虽然到plotly或开罗使用哪个。

+2

也许我想得简单,但你有没有想过生成HTML页面,并使用CSS来安排箱子?当窗口尺寸改变时会允许动态布局吗? – Arminius

+0

这实际上有很大的意义......我是新手,对于像我这样的问题有很多答案,我只是迷失在他们身上。任何建议关于这个主题的教程?把情节/酒吧放在同一页面你会说什么是最好的? – user3755529

+1

作为Aminius建议,您可以使用[HTML的子集和CSS(https://doc.qt.io/qt-5/richtext-html-subset.html)格式的文本,并把它放在一个'QLabel' 。布局文本的最简单方法是使用html表格。 – ekhumoro

回答

2

尽管如上所述,您可能更喜欢使用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字符的箭头,这将是一个可怕的很简单。

+1

在其他纯文本显示中有可用于三角形的Unicode字符。 –

+0

更新,以反映 –

+0

Tkinter的不是标签 – eyllanesc