Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Tkinter-AttributeError中使用变量值:“unicode”对象没有属性“tk”_Python_Json_Tkinter - Fatal编程技术网

Python 在Tkinter-AttributeError中使用变量值:“unicode”对象没有属性“tk”

Python 在Tkinter-AttributeError中使用变量值:“unicode”对象没有属性“tk”,python,json,tkinter,Python,Json,Tkinter,和Tkinter有点问题 我想使用JSON文件作为配置来分配小部件的框架 我的JSON解析器是: with open('data.txt') as json_file: data = json.load(json_file) for p in data['config']: clockFrame = (p['clockFrame']) clockSide = (p['clockSide']) 当使用print clockFrame运行测试时,它会生成我想要的self.topFrame

和Tkinter有点问题

我想使用JSON文件作为配置来分配小部件的框架

我的JSON解析器是:

 with open('data.txt') as json_file:
 data = json.load(json_file)
 for p in data['config']:
 clockFrame = (p['clockFrame'])
 clockSide = (p['clockSide'])
当使用print clockFrame运行测试时,它会生成我想要的self.topFrame值

这一行代码按预期工作:

self.clock = clock.Clock(self.topFrame)
但是,当我想使用变量时,例如:

self.clock = clock.Clock(clockFrame)
它最终给了我一个错误:

AttributeError: 'unicode' object has no attribute 'tk'
有人知道调用变量以便我使用该值的最佳方法是什么吗


感谢您的高级支持。

您想配置哪个小部件获取时钟。因为要存储字符串,所以需要某种方法将该字符串映射到帧对象。你的想法是对的,在这种情况下你需要self.topFrame。由于对象的成员数据已经是一个命名的排序数据库,您只需将配置更改为包含所需对象的变量的名称即可

with open('data.txt') as json_file:
    data = json.load(json_file)
    # this loops through a list of configs and keeps the last
    # one, which is odd..
    for p in data['config']:
        clockFrame = (p['clockFrame'])
        clockSide = (p['clockSide'])

     # in this example, clockFrame = "topFrame"

# get clock frame from current object data
self.clock = clock.Clock(getattr(self, clockFrame))

请检查self.topFrame和clockFrame是否具有相同的类型。什么是clock.clock及其初始化参数是什么?您从json配置中读取一个字符串。这与Clock的调用签名匹配吗?顺便说一句,您似乎正在使用Python2.x,它已经到了生命的尽头。考虑升级到Python 3。@ tDelaNy将把代码移植到Python 3。就代码而言,时钟内部不应该有任何与此相关的内容。self.topFrame=Frameself.tk,background='black'是topFrame的代码。JSON只是clockFrame:self.topframe,这是一种奇怪的配置方式。它不是self.topFrame,只是一个字符串self.topFrame,您必须以某种方式与框架对象关联。您可以更改json配置吗?假设它只是topFrame,那么您可以self.clock=clock.Clockgetattrself,clockFrame