【发布时间】:2020-07-15 03:20:04
【问题描述】:
我一直在寻找一个很好的 python 模块来处理表格。过去,我做了一个个人 C# 项目,使用表格非常简单。
我发现我的解决方案可能在 dmnfarrell 的“tkintertable”存储库中: https://github.com/dmnfarrell/tkintertable
-
我目前正在使用 python 3.7
-
我的python3.7中安装的默认
Tcl库是Tcl8.6。
于是我安装了存储库并开始尝试,这是一个短代码sn-p:
from tkintertable import TableCanvas, TableModel
from tkinter import *
from tkintertable.Testing import sampledata
data = sampledata()
"""data = {0: {'a': 0.85, 'b': 0.89, 'c': 0.01, 'd': 0.98, 'e': 0.88},
1: {'a': 0.14, 'b': 0.52, 'c': 0.68, 'd': 0.68, 'e': 0.74},
2: {'a': 0.75, 'b': 0.23, 'c': 0.92, 'd': 0.1, 'e': 0.42},
3: {'a': 0.15, 'b': 0.2, 'c': 0.37, 'd': 0.96,'e': 0.08},
4: {'a': 0.44, 'b': 0.41, 'c': 0.29, 'd': 0.16, 'e': 0.05},
5: {'a': 0.7, 'b': 0.52, 'c': 0.3, 'd': 0.55, 'e': 0.01},
6: {'a': 0.9, 'b': 0.63, 'c': 0.29, 'd': 0.66, 'e': 0.76},
7: {'a': 0.19, 'b': 0.78, 'c': 0.04, 'd': 0.67, 'e': 0.41},
8: {'a': 0.33, 'b': 0.94, 'c': 0.02, 'd': 0.38, 'e': 0.33},
9: {'a': 0.81, 'b': 0.58, 'c': 0.57, 'd': 0.9, 'e': 0.89}}
"""
table = TableCanvas(tframe,
data=data,
cellbackgr='#F1EFEF',
thefont=('Arial', 12),
rowheight=30,
bg="red",
reverseorder=1,
grid_color="black",
selectedcolor="gray",
multipleselectioncolor="#CCCCFF",
)
table.show()
在某些时候,这工作得很好,但过了一段时间,我开始收到这个错误:
File "C:\MRES1\AppData\Roaming\Python\Python37\site-packages\tkintertable\Tables.py", line
1534, in drawGrid
fill=self.grid_color, width=self.linewidth)
File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 2489, in create_line
return self._create('line', args, kw)
File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 2477, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: bad screen distance "1.0"
所以我开始寻找这个错误并遇到了这个:Matplotlib - _tkinter.TclError: bad screen distance "320.0"
所以我告诉自己“让我们试试这个”我不知道在哪里改变它所以我跑(我知道这可能是一个错误)int 到 @987654327 的实现@class 并查找错误日志中给出的行。那将是1534。
File "C:\MRES1\AppData\Roaming\Python\Python37\site-packages\tkintertable\Tables.py", line 1534, in drawGrid fill=self.grid_color, width=self.linewidth)
在 class 内部,实际上来自 dmnfarrell 的存储库,我做了一些更改以使其正常工作。 我并不是说这是一个好习惯,但错误停止了。
旧
if self.vertlines==1:
for col in range(cols+1):
x=self.col_positions[col]
self.create_line(x,y_start,x,y_start+rows*h, tag='gridline',
fill=self.grid_color, width=self.linewidth)
if self.horizlines==1:
for row in range(startrow, endrow+1):
y_pos=y_start+row*h
self.create_line(x_start,y_pos,self.tablewidth,y_pos, tag='gridline',
fill=self.grid_color, width=self.linewidth)
新
if self.vertlines==1:
for col in range(cols+1):
x=self.col_positions[col]
self.create_line(x,y_start,x,y_start+rows*h, tag='gridline',
fill=self.grid_color, width=int(self.linewidth))
if self.horizlines==1:
for row in range(startrow, endrow+1):
y_pos=y_start+row*h
self.create_line(x_start,y_pos,self.tablewidth,y_pos, tag='gridline',
fill=self.grid_color, width=int(self.linewidth))
请注意,唯一改变的是 width 字段中的 int() 转换。
这种方法效果很好直到我将我的 数据 更改为来自 sqlite3 数据库的更真实的东西,其中包含更多字段和元素。所以我给数据提供了所需的格式来填充我的表格,然后再一次:
_tkinter.TclError: bad screen distance "1323.5"
但这次错误发生在 TableCanvas 类的另一部分,所以我基本上去了那里,并在另一种情况下做了同样的事情来解决这个问题并再次正常工作。
这是确切的部分:
File "C:\Users\MRES1\AppData\Roaming\Python\Python37\site-packages\tkintertable\Tables.py", line 332, in redraw
self.configure(scrollregion=(0,0, self.table.tablewidth+self.table.x_start, self.height))
所以我再一次转换为int() 那里使用的width:
self.tablewidth = int(self.tablewidth) # Basically here is where I make the convertion.
# The line below is where I figured to be a good starting point to deal with the error.
self.configure(scrollregion=(0,0, self.tablewidth+self.x_start, self.rowheight*self.rows+10))
我知道这可能是解决此问题的最糟糕的解决方法之一,我知道我是新手。 只是想从你们那里得到一些建议,以便正确使用这个模块,如果这不可能,如果你能给我任何建议,那就太好了。
我知道我可能不会使用正确的词汇,也欢迎对此提出任何建议。 谢谢大家!
【问题讨论】:
-
该代码在我的 Python 3.7 和 tkintertable 1.3.2 中运行良好。
标签: python tkinter python-3.7 tkintertable