Package teamwork :: Package widgets :: Package PsychGUI :: Package AgentWindow :: Module PropertyPane
[hide private]
[frames] | no frames]

Source Code for Module teamwork.widgets.PsychGUI.AgentWindow.PropertyPane

  1  from Tkinter import * 
  2  import tkMessageBox 
  3  import Pmw 
  4  from tkFileDialog import askopenfilename 
  5  from teamwork.widgets.images import getImageDirectory,getImage 
  6   
7 -class PropertyFrame(Pmw.ScrolledFrame):
8 """Frame for managing general properties of an entity (e.g., image, 9 description, horizon, belief depth 10 """ 11
12 - def __init__(self,parent,entity,**kw):
13 optiondefs = ( 14 ('balloon',None,Pmw.INITOPT), 15 ('generic',False,Pmw.INITOPT), 16 ('expert', False,None), 17 ('step',lambda : None,None), 18 ('policy',lambda entity: None,None), 19 ('society',{},None), 20 ('valueCmd', None,None), 21 ) 22 self.entity = entity 23 self.defineoptions(kw, optiondefs) 24 Pmw.ScrolledFrame.__init__(self,parent) 25 self.interior().grid_columnconfigure(1,weight=1) 26 # Entity image 27 try: 28 image = PhotoImage(file=self.entity.attributes['imageName']) 29 except KeyError: 30 image = PhotoImage(file=getImage('nobody.gif')) 31 self.entity.attributes['image'] = image 32 widget = self.createcomponent('%s Image' % (self.entity.ancestry()), 33 (),None,Button,(self.interior(),)) 34 widget.configure(command=self.selectImage) 35 widget.configure(image=self.entity.attributes['image'], 36 width=100,height=100) 37 widget.grid(row=0,column=0) 38 # # Draw description entry 39 # widget = self.createcomponent('description',(),None,Pmw.ScrolledText, 40 # (self.interior(),), 41 # hscrollmode='none', 42 # text_width=40,text_height=10, 43 # text_wrap='word') 44 # try: 45 # widget.settext(text=self.entity.description) 46 # except AttributeError: 47 # widget.settext(text=self.entity.name) 48 # widget.component('text').focus_set() 49 # widget.grid(row=0,column=1,sticky='ewns',padx=10,pady=10) 50 # Button area 51 frame = Frame(self.interior()) 52 if self['generic']: 53 if not self.entity.parent: 54 widget = Label(frame,text='Horizon:',justify='left') 55 widget.grid(row=0,column=0,sticky='w') 56 self.createcomponent('Horizon',(),None,Pmw.Counter,(frame,), 57 entryfield_modifiedcommand=self.getHorizon, 58 entryfield_value=self.entity.horizon, 59 entryfield_validate={'validator':'integer', 60 'min':1}, 61 ).grid(row=0,column=1,sticky='w') 62 if self['balloon']: 63 self['balloon'].bind(widget,'The number of rounds into the future this agent considers when choosing an action.') 64 self['balloon'].bind(self.component('Horizon'),'The number of rounds into the future this agent considers when choosing an action.') 65 widget = Label(frame,text='Belief Depth:',justify='left') 66 widget.grid(row=1,column=0,sticky='w') 67 self.createcomponent('Depth',(),None,Pmw.Counter,(frame,), 68 entryfield_modifiedcommand=self.getDepth, 69 entryfield_value=self.entity.depth, 70 entryfield_validate={'validator':'integer', 71 'min':0}, 72 ).grid(row=1,column=1,sticky='w') 73 if self['balloon']: 74 self['balloon'].bind(widget,'The number of levels of recursive beliefs that this agent will maintain of others.') 75 self['balloon'].bind(self.component('Depth'),'The number of levels of recursive beliefs that this agent will maintain of others.') 76 elif len(self.entity.actions.getOptions()) > 0: 77 # Button box for controlling this agent 78 widget = self.createcomponent('%s Run Box' % (self.entity.ancestry()), 79 (), None,Pmw.ButtonBox,(frame,), 80 orient='vertical') 81 if not self.entity.parent: 82 # Can't run a real step for a subjective agent 83 widget.add('real',text='Real Step',command=self.stepEntity) 84 widget.add('hypo',text='Hypothetical Step',command=self.applyPolicy) 85 if len(self.entity.getGoals()) > 0 and self['valueCmd']: 86 # Add an expected value button 87 widget.add('value',text='Expected Value', 88 command = lambda s=self,e=self.entity:s.value(e)) 89 widget.grid(row=0,column=1) 90 frame.grid(row=0,column=1,sticky='ewns') 91 self.initialiseoptions()
92
93 - def selectImage(self):
94 """Pops up a dialog to allow user to select image for an entity""" 95 filename = askopenfilename(parent=self._hull, 96 initialdir = getImageDirectory()) 97 if filename: 98 try: 99 image = PhotoImage(file=filename) 100 except: 101 image = None 102 if image: 103 widget = self.component('%s Image' % (self.entity.ancestry())) 104 widget.configure(image=image) 105 self.entity.attributes['image'] = image 106 self.entity.attributes['imageName'] = filename
107
108 - def getHorizon(self):
109 widget = self.component('Horizon') 110 horizon = int(widget.get()) 111 self.entity.horizon = horizon
112
113 - def getDepth(self):
114 widget = self.component('Depth') 115 depth = int(widget.get()) 116 self.entity.depth = depth
117
118 - def stepEntity(self):
119 self['step']()
120
121 - def applyPolicy(self):
122 self['policy'](self.entity)
123