【问题标题】:Best Module to Layout Text in Python在 Python 中布局文本的最佳模块
【发布时间】:2018-01-22 20:37:00
【问题描述】:

我正在寻找一个程序,它可以输出一堆(大约 30 个)框,如下图所示:

我已经研究了几个星期,我认为要以令人愉悦的图形方式布局文本,我可以在 PyQT 中使用 QTableWidget,但我现在意识到对于这样一个简单的任务来说学习太难了,必须有一个更快的方式。所以我现在正在考虑传递给 Tkinter,或者只是使用像 PyCairo 这样的绘图模块来绘制信息,然后才将每个图像放在 PyQT 界面中。整理绘图模块中的所有定位比学习如何在 PyQT 中做同样的事情要快得多。

但我觉得我遗漏了一些东西,我本来想以一种很好的方式以重复格式排列一堆数字会更容易。

一些盒子还需要一些图形内容,我想用条形图或开罗图来显示它们。

【问题讨论】:

  • 可能我想的太简单了,但是你有没有想过生成一个 HTML 页面并使用 CSS 来排列盒子?当窗口尺寸也发生变化时是否允许动态布局?
  • 这实际上很有意义......我是新手,对于像我这样的问题有很多答案,我只是迷失了它们。关于这个主题的教程有什么建议吗?将图/条放在同一页面中,你会说什么是最好的?
  • 按照 Aminius 的建议,您可以使用 Subset of HTML and CSS 格式化文本并将其放入 QLabel。布局文本最简单的方法是使用 html 表格。
  • Django 与这一切有什么关系吗?还是没有必要?
  • @user3755529。不,您只是想寻找一个非常基本的 html 教程。互联网上可能有成千上万这样的事情。

标签: python layout pyqt cairo graphic


【解决方案1】:

虽然您可能最好使用上面提到的 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 字符作为箭头,这将是非常简单。

【讨论】:

  • 有 unicode 字符可用于在其他纯文本显示中的三角形。
  • 已更新以反映
  • Tkinter 不在标签内
  • @eyllanesc 不,不是,但是 OP 说“所以我现在正在考虑传递给 Tkinter,或者可能只是使用像 PyCairo 这样的绘图模块绘制信息,然后才将每个图像放在 PyQT界面。”。因此,我提供了一个使用 tkinter 来证明它是可能的解决方案。
  • 太棒了,有很多信息让我开始得到我想要的东西。如果有人有兴趣在这里看到完整的故事和我的发展,那就是github.com/AskBid/scrap-custom-trading-watchlist,警告 n00b 编码
猜你喜欢
  • 2011-05-06
  • 2015-09-16
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多