未填写tkinter gui

时间:2018-05-23 21:21:15

标签: python tkinter

我正在编写一个程序,它将从网站上抓取数据,并将数据放在tkinter gui的表格中。

我找到了制作桌子的这个方法:https://code.activestate.com/recipes/580793-tkinter-table-with-scrollbars/这对我想做的事情来说是完美的。

问题是我输入的值没有填满行。它们包装得太早,使得信息难以阅读。我该如何解决?

示例:

Picture of a table

相关代码:

from tkinter import *
import tabless # Name of the script linked above

root = Tk()
table = tabless.Table(root, ["Title 1", "Title 2", "Title 3", "Title 4"], column_minwidths=[200, 350, 100, 100])
table.pack(padx=10,pady=10,side=LEFT)
table.set_data([[1,2,3,4],[5,6,7,8], [9,10,11,12], [13,14,15,16], [17,18,19,20],[21,22,23,24], [25,26,27,28], [29,30,31,32], [33,34,35,36], [37,38,39,40], [41,42,43,44], [45,46,47,48], [49,50,51,52], [53,54,55,56], [57,58,59,60], [61,62,63,64], [65,66,67,68], [69,70,71,72], [73,74,75,76], [77,78,79,80], [81,82,83,84], [85,86,87,88], [89,90,91,92], [93,94,95,96], [97,98,99,100], [101,102,103,104], [105,106,107,108], [109,110,111,112], [113,114,115,116], [117,118,119,120], [121,122,123,124], [125,126,127,128], [129,130,131,132], [133,134,135,136], [137,138,139,140], [141,142,143,144], [145,146,147,148], [149,150,151,152], [153,154,155,156], [157,158,159,160], [161,162,163,164], [165,166,167,168], [169,170,171,172], [173,174,175,176], [177,178,179,180], [181,182,183,184], [185,186,187,188], [189,190,191,192], [193,194,195,196], [197,198,199,200]])
example_list = []
for i in range(0, 50):
    example_list.append(['thisisalongnumber12123123123', True, 'thisisashorternumber12123123123', 'thisisaname', 'thisisaverylongtitle12345678901111', 'number', 'number'])
for count, i in enumerate(example_list):
    table.cell(count, 0, i[3])
    table.cell(count, 1, i[4])
    table.cell(count, 2, i[5])
    table.cell(count, 3, i[6])

1 个答案:

答案 0 :(得分:0)

我将 tkinter.Message 换成了一个简单的 tkinter.Label,“wraplength”参数为 300。这为我解决了这个问题。

这里是修改后的DataCell类

class Data_Cell(Cell):
    def __init__(self, master, variable, anchor=W, bordercolor=None, 
             borderwidth=1, padx=0, pady=0, 
             background=None, foreground=None, font=None):
    Cell.__init__(self, master, background=background,
                  highlightbackground=bordercolor, 
                  highlightcolor=bordercolor, 
                  highlightthickness=borderwidth, bd=0)

    self._message_widget = Label(self, textvariable=variable, 
                                 font=font, background=background,
                                 foreground=foreground,
                                 wraplength=300)
    self._message_widget.pack(expand=True, padx=padx, pady=pady,
                              anchor=anchor)