def closewin():pass
def draw():
load_start_time = time.time()
with open("./StartUp.log","r",encoding = "utf-8") as f:
text = json.loads(f.read())
pb["value"] = 10
win.update()
namelist = []
valuelist = []
for i in range(len(text)):
valuelist.append([])
for k,v in text[i].items():
if not k in namelist:
namelist.append(k)
valuelist[i].append(v)
#print(namelist)
#print(valuelist)
pb["value"] = 20
win.update()
# 生成一个包含随机数据的表格
plt.rcParams['font.family'] = 'SimHei'
d = pd.DataFrame(valuelist, columns=namelist).round(2)
pb["value"] = 30
win.update()
fig, ax = plt.subplots(figsize=(len(namelist) * 4.2,len(valuelist) + 2))
pb["value"] = 35
win.update()
# 基于pandas表格数据创建和展示图形表格
column_definitions = []
for i in namelist:
column_definitions.append(ColumnDefinition(name = i,border = "l"))
column_definitions.append(ColumnDefinition(name = i,border = "both"))
pb["value"] = 40
win.update()
tab = Table(d,column_definitions = column_definitions)
pb["value"] = 50
win.update()
# 保存图片
plt.savefig("table.jpg", dpi=300,bbox_inches='tight')
pb["value"] = 90
win.update()
os.startfile("table.jpg")
#plt.show()
plt.close()
pb["value"] = 100
pl.config(text = "可以关闭!")
win.update()
win.protocol("WM_DELETE_WINDOW",partial(win.destroy))
load_end_time = time.time()
print("数据加载时间:",load_end_time - load_start_time)
在数据过多时,python plt.subplots创建画布时过大,导致报错
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 749, in callit
func(*args)
File "C:\Users\Administrator\Desktop\PYExe\2024项目\系统工具\开机启动\StartUp_Log_Viewer.py", line 96, in draw
fig, ax = plt.subplots(figsize=(len(namelist) * 4.2,len(valuelist) + 2))
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\pyplot.py", line 1453, in subplots
fig = figure(**fig_kw)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\pyplot.py", line 809, in figure
FigureClass=FigureClass, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\pyplot.py", line 325, in new_figure_manager
return _get_backend_mod().new_figure_manager(*args, **kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backend_bases.py", line 3494, in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\_backend_tk.py", line 965, in new_figure_manager_given_figure
canvas = cls.FigureCanvas(figure, master=window)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\_api\deprecation.py", line 389, in wrapper
return func(*inner_args, **inner_kwargs)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\_backend_tk.py", line 176, in __init__
master=self._tkcanvas, width=w, height=h)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3545, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 3501, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: not enough free memory for image buffer
或者让其不需要展示到plt中就导出?(不要修改画布大小,不然内容装不下的)