目标很简单:显示一个带有选项的对话框,然后当选择一个选项时,它将自动替换一些值(以减少手动输入值时出错的可能性)。下面的代码是较大代码的一部分,但这是最重要的部分。更大的代码是由其他人编写的。我写了以下块:
if expInfo2['quadrant'] == 'UL':
expInfo['refOrientation':'45','x':'-4.24','y':'4.24']
elif expInfo2['quadrant'] == 'LL':
expInfo['refOrientation':'-45','x':'-4.24','y':'-4.24']
elif expInfo2['quadrant'] == 'UR':
expInfo['refOrientation':'-45','x':'4.24','y':'4.24']
elif expInfo2['quadrant'] == 'LR':
expInfo['refOrientation':'45','x':'4.24','y':'-4.24']代码可以正常工作,直到读取第二行,并给出以下错误:
Traceback (most recent call last): File "D:\User\File\Experiment.py", line 48, in <module> expInfo['refOrientation':'45','x':'-4.24','y':'4.24'] TypeError: unhashable type ()
我在编程方面的经验是有限的,但我明白,我在第二行中所做的事情并不合二为一。不过,我想把它们一分为二,但我不认为这会奏效,如下所示:
if expInfo2['quadrant'] == 'UL':
expInfo['refOrientation':'45']
expInfo['x':'-4.24']
expInfo['y':'4.24']
et cetera...完整的代码:
#present a dialogue to chose lab room and whether eyetracker is on or not
expInfo2 = {'lab':'2','eyetracker': '0','quadrant':''}
dlg = gui.Dlg(title="Info", pos=(200, 400))
dlg.addField('Which lab are you in? 2 for lab-2, 3 for lab-3',expInfo2['eyelab'])
dlg.addField('Do you want the eyetracker on? 0 for yes, 1 for no',expInfo2['eyetracker'])
dlg.addField('What quadrant is used? UL=Upper Left, LL=Lower Left, UR=Upper Right, LR=Lower Right',expInfo2['quadrant'])
inf = dlg.show()
expInfo2['lab']=inf[0]
expInfo2['eyetracker'] = inf[1]
expInfo2['quadrant'] = inf[2]
############################## THIS IS THE CODE FOR LAB 2 ###########################################
if expInfo2['lab'] == '2':
expInfo = {'observer':'insert','typeofstaircase':'insert','refOrientation':'','startorient':'insert','x':'','y':'','numstair':4,}
dateStr = time.strftime("%b_%d_%H%M", time.localtime())#add the current time
if expInfo2['quadrant'] == 'UL':
expInfo['refOrientation':'45','x':'-4.24','y':'4.24']
elif expInfo2['quadrant'] == 'LL':
expInfo['refOrientation':'-45','x':'-4.24','y':'-4.24']
elif expInfo2['quadrant'] == 'UR':
expInfo['refOrientation':'-45','x':'4.24','y':'4.24']
elif expInfo2['quadrant'] == 'LR':
expInfo['refOrientation':'45','x':'4.24','y':'-4.24']
#present a dialogue to change params
dlg = gui.Dlg(title="Info", pos=(200, 400))
dlg.addField('Observer:',expInfo['observer'])
dlg.addField('Type of staircase?', expInfo['typeofstaircase'])
dlg.addField('Start Orientation Increment:',expInfo['startorient'])
dlg.addField('X:',expInfo['x'])
dlg.addField('Y:',expInfo['y'])
dlg.addField('Ref. Orienation:',expInfo['refOrientation'])
#dlg.addField('Number of Staircases',expInfo['numstair'])
inf = dlg.show()
expInfo['observer']=inf[0]
expInfo['typeofstaircase'] = inf[1]
expInfo['startorient']=inf[2]
expInfo['x']=inf[3]
expInfo['y']=inf[4]
expInfo['refOrientation']=inf[5]
#expInfo['numstair'] = inf[6]
#dlg = gui.DlgFromDict(expInfo, title='info', fixed=['date'])
#if dlg.OK:
# print(expInfo)
#else:
# core.quit()#the user hit cancel so exit有什么建议吗?
发布于 2018-06-18 13:40:05
expInfo['refOrientation':'-45','x':'-4.24','y':'-4.24']所以这被解释为
expInfo.__getitem__((
slice('refOrientation', '-45', None),
slice('x', '-4.24', None),
slice('y', '-4.24', None)
)),切片对象的三个元组。expInfo是一本字典,它只采用可接受的类型和
hash((
slice('refOrientation', '-45', None),
slice('x', '-4.24', None),
slice('y', '-4.24', None)
))引发错误
TypeError: unhashable type: 'slice'因为,他们不是哈斯德。在这样的切片中使用字符串是非常非常奇怪的,所以我认为您没有以正确的方式输入键。
我想你想做的是更新字典。要做到这一点,你想:
if expInfo2['quadrant'] == 'UL':
expInfo.update({'refOrientation': '45', 'x': '-4.24', 'y': '4.24'})
elif expInfo2['quadrant'] == 'LL':
...(请注意我输入的空格,以使其更易读。我建议在使用python之前先阅读PEP8,因为您将样式指南抛出了窗口)。
https://stackoverflow.com/questions/50910718
复制相似问题