在会话之间保存tkinter小部件参数

2024-05-15 23:54:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个tkinter table应用程序,需要在会话或关机期间保留其内容。为此,我基于widget.config()和grid_info()方法,以dicts格式将应用程序的内容写入文件。其想法是,在恢复时,我可以使用字典和为每个小部件保存的参数重建表。但我遇到了一个麻烦,希望你能帮我

{'activebackground': '#ececec', 'activeforeground': '#000000', 'anchor': 'center',
 'background': 'white', 'bd': <pixel object: '1'>, 'bg': 'white', 'bitmap': '',
 'borderwidth': <pixel object: '1'>, 'compound': 'none', 'cursor': '',
 'disabledforeground': '#a3a3a3', 'fg': '#000000', 'font': 'TkDefaultFont',
 'foreground': '#000000', 'height': 1, 'highlightbackground': '#d9d9d9',
 'highlightcolor': '#000000', 'highlightthickness': <pixel object: '0'>,
 'image': '', 'justify': 'center', 'padx': <pixel object: '1'>, 'pady': <pixel object: '1'>,
 'relief': 'raised', 'state': 'normal', 'takefocus': '0', 'text': 'C1', 'textvariable': '',
 'underline': -1, 'width': 6, 'wraplength': <pixel object: '0'>}

上面是标签小部件的字典。困难在于那些读取<pixel obj ...>的值,这会导致语法错误。如果我从dict中删除这些条目,eval()将毫无疑问地处理它

如果我能说明在编码/解码过程中对这些值的特殊处理,一种更优雅的方法是以JSON格式保存文件。我该怎么做有什么提示吗


Tags: 文件方法应用程序内容字典object部件tkinter