Package Pmv :: Module stringSelectorGUI
[hide private]
[frames] | no frames]

Source Code for Module Pmv.stringSelectorGUI

  1  ############################################################################# 
  2  # 
  3  # Author: Ruth HUEY, Michel SANNER 
  4  # 
  5  # Copyright: M. Sanner TSRI 2000 
  6  # 
  7  ############################################################################# 
  8   
  9  import Numeric, Tkinter, types, string 
 10  from DejaVu.viewerFns import checkKeywords 
 11  from MolKit.molecule import MoleculeSet, Atom, AtomSetSelector 
 12  from MolKit.protein import Protein, ProteinSetSelector, Residue, \ 
 13                  ResidueSetSelector, Chain, ChainSetSelector 
 14  import tkMessageBox, tkSimpleDialog 
 15   
 16  from MolKit.stringSelector import StringSelector 
 17   
 18   
 19   
20 -class StringSelectorGUI(Tkinter.Frame):
21 """ molEntry, chainEntry, resEntry, atomEntry 22 molMB, chainMB, resSetMB, atomSetMB, #setsMB 23 showBut, 24 clearBut, acceptBut, closeBut 25 """ 26 27 entryKeywords = [ 28 'molWids', 29 'molLabel', 30 'molEntry', 31 'chainWids', 32 'chainLabel', 33 'chainEntry', 34 'resWids', 35 'resLabel', 36 'resEntry', 37 'atomWids', 38 'atomLabel', 39 'atomEntry', 40 ] 41 42 menuKeywords = [ 43 'molMB', 44 'chainMB', 45 'resSetMB', 46 'atomSetMB', 47 ] 48 49 buttonKeywords = [ 50 'showBut', 51 'clearBut', 52 ] 53 54 masterbuttonKeywords = [ 55 'acceptBut', 56 'closeBut' 57 ] 58 59
60 - def __init__(self, master, check=0, molSet=MoleculeSet([]), 61 userPref = 'cS', vf = None, all=1, crColor=(0.,1.,0), 62 clearButton=True, showButton=True, sets=None, **kw):
63 64 if not kw.get('packCfg'): 65 self.packCfg = {'side':'top', 'anchor':'w','fill':'x'} 66 67 self.sets = sets 68 #all is a shortcut to use all of default values 69 if 'all' in kw.keys(): 70 all = 1 71 elif __debug__: 72 if check: 73 apply( checkKeywords, ('stringSelector', self.entryKeywords), kw) 74 75 Tkinter.Frame.__init__(self, master) 76 ##???? 77 Tkinter.Pack.config(self, side='left', anchor='w') 78 79 80 #to make a stringSelector need vf, moleculeSet, selString 81 self.master = master 82 self.molSet = molSet 83 self.residueSelector = ResidueSetSelector() 84 self.atomSelector = AtomSetSelector() 85 self.userPref = userPref 86 self.vf = vf 87 if not self.vf is None: 88 self.addGeom(crColor) 89 else: 90 self.showCross = None 91 self.molCts = {} 92 self.chainCts = {} 93 94 # optsDict includes defaults for all possible widgets 95 # in practice 96 # user can specify which widgets to put into gui 97 d = self.optsDict = {} 98 99 #first check for menubuttons: 100 101 llists = [self.entryKeywords, self.menuKeywords, self.buttonKeywords] 102 if all: 103 if not clearButton: 104 self.buttonKeywords.remove("clearBut") 105 if not showButton: 106 self.buttonKeywords.remove("showBut") 107 for l in llists: 108 for key in l: 109 d[key] = 1 110 111 else: 112 for l in llists: 113 for key in l: 114 d[key] = kw.get(key) 115 116 117 118 self.flexChildren = { 119 'mol': ['molLabel', 'molEntry'], 120 'chain': ['chainLabel', 'chainEntry'], 121 'res': ['resLabel', 'resEntry'], 122 'atom': ['atomLabel', 'atomEntry'], 123 } 124 125 for w in ['molWids', 'chainWids','resWids','atomWids']: 126 if kw.get(w): 127 lab = w[:-4] 128 s = lab+'Label' 129 d[s] = 1 130 s1 = lab+'Entry' 131 d[s1] = 1 132 133 134 # instantiate all Tkinter variables (?) 135 #DON"T HAVE ANY??? 136 self.buildTkVars() 137 138 # only build requested widgets(?) 139 self.buildifdDict() 140 141 # build commandDictionary 142 self.buildCmdDict() 143 144 self.buildGUI()
145 146 147
148 - def buildifdDict(self):
149 #to prevent dependency on InputFormDescr: put all widgets in a frame 150 #this dictionary has default values for each widget 151 self.ifdDict = {} 152 self.ifdDict['topFrame'] = {'name': 'topFrame', 153 'widgetType':Tkinter.Frame, 154 'wcfg':{'relief': 'flat', 'bd':2}, 155 'packCfg':{'side':'top', 'fill':'x'} 156 } 157 self.ifdDict['buttonFrame'] = {'name': 'buttonFrame', 158 'widgetType':Tkinter.Frame, 159 'wcfg':{'relief': 'flat', 'bd':2}, 160 'packCfg':{'side':'top', 'fill':'x'} 161 } 162 self.ifdDict['entryFrame'] = {'name': 'entryFrame', 163 'widgetType':Tkinter.Frame, 164 'wcfg':{'relief': 'flat', 'bd':2}, 165 'packCfg':{'side':'left'} 166 } 167 self.ifdDict['menuFrame'] = {'name': 'menuFrame', 168 'widgetType':Tkinter.Frame, 169 'wcfg':{'relief': 'flat', 'bd':2}, 170 'packCfg':{'side':'left', 'fill':'x'} 171 } 172 self.ifdDict['molWids'] = {'name': 'molWids', 173 'widgetType': Tkinter.Frame, 174 'wcfg':{'relief': 'flat'}, 175 'children': ['molLabel', 'molEntry'], 176 'packCfg':{'side':'top'} 177 } 178 self.ifdDict['molLabel'] = {'name': 'molLabel', 179 'widgetType':Tkinter.Label, 180 'wcfg':{'text': 'Molecule'}, 181 'packCfg':{'side':'left', 'anchor': 'w', 'fill':'x'} 182 } 183 self.ifdDict['molEntry'] = {'name': 'molEntry', 184 'widgetType':Tkinter.Entry, 185 'wcfg':{'bd': 3}, 186 'packCfg':{'side':'left', 'anchor':'e', 'fill':'x'} 187 } 188 self.ifdDict['chainWids'] = {'name': 'chainWids', 189 'widgetType': Tkinter.Frame, 190 'wcfg':{'relief': 'flat', 'bd':3}, 191 'children': ['chainLabel', 'chainEntry'], 192 'packCfg':{'side':'top'} 193 } 194 self.ifdDict['chainLabel'] = {'name': 'chainLabel', 195 'widgetType':Tkinter.Label, 196 'wcfg':{'text': 'Chain '}, 197 'packCfg':{'anchor': 'w', 'side':'left'} 198 } 199 self.ifdDict['chainEntry'] = {'name': 'chainEntry', 200 'widgetType':Tkinter.Entry, 201 'wcfg':{'bd': 3}, 202 'packCfg':{'side':'left', 'anchor':'e', 'fill':'x'} 203 } 204 self.ifdDict['resWids'] = {'name': 'resWids', 205 'widgetType': Tkinter.Frame, 206 'wcfg':{'relief': 'flat'}, 207 'children': ['resLabel', 'resEntry'], 208 'packCfg':{'side':'top'} 209 } 210 self.ifdDict['resLabel'] = {'name': 'resLabel', 211 'widgetType':Tkinter.Label, 212 'wcfg':{'text': 'Residue'}, 213 'packCfg':{'anchor': 'w', 'side':'left'} 214 } 215 self.ifdDict['resEntry'] = {'name': 'resEntry', 216 'widgetType':Tkinter.Entry, 217 'wcfg':{'bd': 3}, 218 'packCfg':{'side':'left', 'anchor':'e', 'fill':'x'} 219 } 220 self.ifdDict['atomWids'] = {'name': 'atomWids', 221 'widgetType': Tkinter.Frame, 222 'wcfg':{'relief': 'flat'}, 223 'children': ['atomLabel', 'atomEntry'], 224 'packCfg':{'side':'top'} 225 } 226 self.ifdDict['atomLabel'] = {'name': 'atomLabel', 227 'widgetType':Tkinter.Label, 228 'wcfg':{'text': 'Atom '}, 229 'packCfg':{'anchor': 'w', 'side':'left'} 230 } 231 self.ifdDict['atomEntry'] = {'name': 'atomEntry', 232 'widgetType':Tkinter.Entry, 233 'wcfg':{'bd': 3}, 234 'packCfg':{'side':'left', 'anchor':'e', 'fill':'x'} 235 } 236 #next the menus 237 self.ifdDict['molMB'] = {'name': 'molMB', 238 'widgetType':Tkinter.Menubutton, 239 'buildMenuCmd': self.buildMolMenu, 240 'wcfg':{ 241 'text': 'Molecule List', 242 'relief': 'ridge'}, 243 'packCfg':{'side':'top', 'anchor':'w', 'fill':'x'}} 244 self.ifdDict['chainMB'] = {'name': 'chainMB', 245 'widgetType':Tkinter.Menubutton, 246 'buildMenuCmd': self.buildChainMenu, 247 'wcfg':{ 248 'text': 'Chain List', 249 'relief': 'ridge'}, 250 'packCfg':{'side':'top', 'anchor':'w', 'fill':'x'}} 251 self.ifdDict['resSetMB'] = {'name': 'resSetMB', 252 'widgetType':Tkinter.Menubutton, 253 'buildMenuCmd': self.buildResMenus, 254 'wcfg':{ 255 'text': 'ResidueSet List', 256 'relief': 'ridge'}, 257 'packCfg':{'side':'top', 'anchor':'w', 'fill':'x'}} 258 self.ifdDict['atomSetMB'] = {'name': 'atomSetMB', 259 'widgetType':Tkinter.Menubutton, 260 'buildMenuCmd': self.buildAtomMenus, 261 'wcfg':{ 262 'text': 'AtomSet List', 263 'relief': 'ridge'}, 264 'packCfg':{'side':'top', 'anchor':'w', 'fill':'x'}} 265 #some way to toggle visual aide 266 self.ifdDict['showBut'] = {'name': 'showBut', 267 'widgetType':Tkinter.Checkbutton, 268 'wcfg':{ 269 'command': self.Show_cb, 270 'variable': self.showCurrent, 271 # should there be a box? 272 #'indicatoron':0, 273 'text':'Show ', 274 'relief':'raised', 275 'bd': '1'}, 276 'packCfg':{'side':'left', 'anchor':'w', 'fill':'x'}} 277 #last the buttons 278 self.ifdDict['clearBut'] = {'name': 'clearBut', 279 'widgetType':Tkinter.Button, 280 'wcfg':{ 281 'command': self.Clear_cb, 282 'text':'Clear Form', 283 'relief':'raised', 284 'bd': '1'}, 285 'packCfg':{'side':'left', 'anchor':'w', 'fill':'x'}} 286 self.ifdDict['acceptBut'] = {'name': 'acceptBut', 287 'widgetType':Tkinter.Button, 288 'wcfg':{ 289 'command': self.get, 290 'text':'Accept', 291 'relief':'raised', 292 'bd': '1'}, 293 'packCfg':{'side':'left', 'anchor':'w', 'fill':'x'}} 294 self.ifdDict['closeBut'] = {'name': 'closeBut', 295 'widgetType':Tkinter.Button, 296 'wcfg':{ 297 'command': self.Close_cb, 298 'text':'Close ', 299 'relief':'raised', 300 'bd': '1'}, 301 'packCfg':{'side':'left', 'anchor':'w', 'fill':'x'}}
302 303
304 - def buildTkVars(self):
305 self.showCurrent = Tkinter.IntVar() 306 self.showCurrent.set(0)
307 #for n in ['showCurrent']: 308 # exec('self.'+ n + '= Tkinter.IntVar()') 309 # exec('self.'+ n + '.set(0)') 310 311
312 - def addGeom(self, crColor):
313 from DejaVu.Points import CrossSet 314 #need to have a new name if there is already a 'strSelCrossSet' 315 # in the viewer... 316 name = 'strSelCrossSet' 317 ctr = 0 318 for c in self.vf.GUI.VIEWER.rootObject.children: 319 if c.name[:14]==name[:14]: 320 ctr = ctr + 1 321 if ctr>0: 322 name = name + str(ctr) 323 self.showCross=CrossSet(name, 324 inheritMaterial=0, materials=(crColor,), 325 offset=0.5, lineWidth=2) 326 self.showCross.Set(visible=1, tagModified=False) 327 self.showCross.pickable = 0 328 self.vf.GUI.VIEWER.AddObject(self.showCross)
329 330
331 - def bindMenuButton(self, mB, command, event='<ButtonPress>', add='+'):
332 mB.bind(event, command, add=add)
333 334
335 - def buildMenu(self, mB, nameList, varDict, oldvarDict, cmd):
336 if nameList: 337 #prune non-valid entries 338 for i in varDict.keys(): 339 if i not in nameList: 340 del varDict[i] 341 del oldvarDict[i] 342 #add anything new 343 for i in nameList: 344 if i not in varDict.keys(): 345 varDict[i]=Tkinter.IntVar() 346 oldvarDict[i]=0 347 else: 348 varDict={} 349 oldvarDict={} 350 #start from scratch and build menu 351 #4/24: only build and add 1 menu 352 if hasattr(mB, 'menu'): 353 mB.menu.delete(1,'end') 354 else: 355 mB.menu = Tkinter.Menu(mB) 356 mB['menu']=mB.menu 357 #PACK all the entries 358 for i in varDict.keys(): 359 mB.menu.add_checkbutton(label=i, var=varDict[i], command=cmd)
360 361
362 - def buildMolMenu(self,event=None):
363 if not hasattr(self,'molVar'): self.molVar={} 364 if not hasattr(self,'oldmolVar'): self.oldmolVar={} 365 molNames = self.molSet.name 366 self.buildMenu(self.molMB,molNames,self.molVar,self.oldmolVar,self.getMolVal)
367 368
369 - def buildChainMenu(self,event=None):
370 if not hasattr(self,'chainVar'): self.chainVar={} 371 if not hasattr(self,'oldchainVar'): self.oldchainVar={} 372 chMols = MoleculeSet([]) 373 if len(self.molSet): 374 chMols=MoleculeSet(filter(lambda x: Chain in x.levels, self.molSet)) 375 chainIDList = [] 376 if len(chMols): 377 chains=chMols.findType(Chain) 378 if chains==None: return 379 for i in chains: 380 chainIDList.append(i.full_name()) 381 self.buildMenu(self.chainMB,chainIDList,self.chainVar,self.oldchainVar,self.getChainVal)
382 383
384 - def buildResMenus(self,event=None):
385 if not hasattr(self,'ResSetsVar'): self.ResSetsVar={} 386 if not hasattr(self,'oldResSetsVar'): self.oldResSetsVar={} 387 ResSetsList = self.residueSelector.residueList.keys() 388 self.residueList = self.residueSelector.residueList 389 #ResSetsList = residueList_.keys() 390 #self.residueList = residueList_ 391 self.buildMenu(self.resSetMB,ResSetsList,self.ResSetsVar,self.oldResSetsVar,self.getResSetsVal)
392 393
394 - def buildAtomMenus(self,event=None):
395 if not hasattr(self,'AtomSetsVar'): self.AtomSetsVar={} 396 if not hasattr(self,'oldAtomSetsVar'): self.oldAtomSetsVar={} 397 AtomSetsList = self.atomSelector.atomList.keys() 398 self.atomList = self.atomSelector.atomList 399 #AtomSetsList = atomList_.keys() 400 #self.atomList = atomList_ 401 self.buildMenu(self.atomSetMB,AtomSetsList,self.AtomSetsVar,self.oldAtomSetsVar,self.getAtomSetsVal)
402 403
404 - def increaseCts(self,dict, newStr):
405 if dict.has_key(newStr): 406 dict[newStr]=dict[newStr]+1 407 else: 408 dict[newStr]=1
409 410
411 - def decreaseCts(self,dict,newStr):
412 if dict.has_key(newStr): 413 currentVal=dict[newStr] 414 if currentVal<=1: currentVal=1 415 dict[newStr]=currentVal-1
416 417
418 - def getMolVal(self, event=None):
419 molWidget = self.molEntry 420 for molStr in self.molVar.keys(): 421 #figure out which check button was just changed 422 newVal=self.molVar[molStr].get() 423 if newVal==self.oldmolVar[molStr]: continue 424 else: 425 self.oldmolVar[molStr]=newVal 426 molList=string.split(molWidget.get(),',') 427 if newVal==1: 428 self.increaseCts(self.molCts,molStr) 429 if not molStr in molList: 430 if molWidget.index('end')==0: 431 molWidget.insert('end',molStr) 432 else: 433 molWidget.insert('end',','+molStr) 434 else: 435 if molStr in molList: 436 self.molCts[molStr]=0 437 molList.remove(molStr) 438 molWidget.delete(0,'end') 439 molWidget.insert('end',string.join(molList,',')) 440 #also turn off all of the chain checkbuttons: 441 chainWidget=self.chainEntry 442 chainList=string.split(chainWidget.get(),',') 443 # chain menu may not have been built yet: 444 if not hasattr(self, 'chainVar'): 445 self.buildChainMenu() 446 for ch in self.chainVar.keys(): 447 chKeyList = string.split(ch,':') 448 thisMolStr=chKeyList[0] 449 thisChainStr=chKeyList[1] 450 #if the chain is in this molecule 451 if thisMolStr==molStr: 452 #turn off chain checkbutton 453 self.chainVar[ch].set(0) 454 self.oldchainVar[ch]=0 455 self.decreaseCts(self.chainCts,thisChainStr) 456 if len(chKeyList)>1 and thisChainStr in chainList: 457 chainList.remove(thisChainStr) 458 chainWidget.delete(0,'end') 459 chainWidget.insert('end',string.join(chainList,','))
460 461
462 - def getChainVal(self, event=None):
463 chains = self.molSet.findType(Chain) 464 molWidget = self.molEntry 465 chainWidget = self.chainEntry 466 for item in self.chainVar.keys(): 467 molStr,chainStr = string.split(item,':') 468 newVal=self.chainVar[item].get() 469 if newVal ==self.oldchainVar[item]: 470 continue 471 else: 472 self.oldchainVar[item]=newVal 473 molList=string.split(molWidget.get(),',') 474 chainList=string.split(chainWidget.get(),',') 475 if newVal==1: 476 self.increaseCts(self.molCts,molStr) 477 self.increaseCts(self.chainCts,chainStr) 478 if not molStr in molList: 479 if molWidget.index('end')==0: 480 molWidget.insert('end',molStr) 481 else: 482 molWidget.insert('end',','+molStr) 483 ###11/17:#if chainStr!=' ' and not chainStr in chainList: 484 if not chainStr in chainList: 485 if chainWidget.index('end')==0: 486 chainWidget.insert('end',chainStr) 487 else: 488 chainWidget.insert('end',','+chainStr) 489 if hasattr(self, 'molVar') and self.molVar.has_key(molStr): 490 self.molVar[molStr].set(1) 491 else: 492 self.buildMolMenu() 493 self.molVar[molStr].set(1) 494 self.oldmolVar[molStr]=1 495 else: 496 if not self.molCts.has_key(molStr): continue 497 if not self.chainCts.has_key(chainStr): continue 498 self.decreaseCts(self.molCts,molStr) 499 self.decreaseCts(self.chainCts,chainStr) 500 chainList=string.split(chainWidget.get(),',') 501 ###11/17:#if chainStr in chainList or chainStr==' ': 502 if chainStr in chainList: 503 if chainStr in chainList and self.chainCts[chainStr]==0: 504 chainList.remove(chainStr) 505 if self.molCts[molStr]==0: 506 if hasattr(self, 'molVar') and self.molVar.has_key(molStr): 507 self.molVar[molStr].set(0) 508 self.oldmolVar[molStr]=0 509 #also remove it from Molecule entry 510 molList=string.split(molWidget.get(),',') 511 if molStr in molList:molList.remove(molStr) 512 newss1=string.join(molList,',') 513 molWidget.delete(0,'end') 514 molWidget.insert('end',newss1) 515 ##also have to fix the Chain entry: 516 chainWidget.delete(0,'end') 517 chainWidget.insert('end',string.join(chainList,','))
518 519
520 - def getResSetsVal(self, event=None):
521 w = self.resEntry 522 ssList = string.split(w.get(),',') 523 for newStr in self.ResSetsVar.keys(): 524 if self.ResSetsVar[newStr].get()==1: 525 if newStr not in ssList: 526 if w.index('end')==0: 527 w.insert('end',newStr) 528 else: 529 w.insert('end',',') 530 w.insert('end',newStr) 531 #method to remove here 532 else: 533 if newStr in ssList: 534 ssList.remove(newStr) 535 w.delete(0,'end') 536 w.insert(0,string.join(ssList,','))
537 538
539 - def getAtomSetsVal(self, event=None):
540 w = self.atomEntry 541 ssList = string.split(w.get(),',') 542 for newStr in self.AtomSetsVar.keys(): 543 if self.AtomSetsVar[newStr].get()==1: 544 if newStr not in ssList: 545 if w.index('end')==0: 546 w.insert('end',newStr) 547 else: 548 w.insert('end',',') 549 w.insert('end',newStr) 550 #method to remove here 551 else: 552 if newStr in ssList: 553 ssList.remove(newStr) 554 w.delete(0,'end') 555 w.insert(0,string.join(ssList,','))
556 557
558 - def getSetsVal(self, event=None):
559 if len(self.sets): 560 sets = self.sets.keys() 561 for newStr in self.setsVar.keys(): 562 node0 = self.sets[newStr][0] 563 ##this would work only w/ 4 levels(/) 564 nodeLevel = node0.isBelow(Protein) 565 l = [self.molEntry, self.chainEntry, self.resEntry, self.atomEntry] 566 w = l[nodeLevel] 567 ssList=string.split(w.get(),',') 568 if self.setsVar[newStr].get()==1: 569 if newStr==' ': continue 570 if not newStr in ssList: 571 if w.index('end')==0: 572 w.insert('end',newStr) 573 else: 574 w.insert('end',',') 575 w.insert('end',newStr) 576 else: 577 if newStr in ssList: 578 ssList.remove(newStr) 579 w.delete(0,'end') 580 w.insert(0,string.join(ssList,','))
581 582
583 - def buildArgs(self, event=None):
584 kw = {} 585 kw['mols'] = self.molEntry.get() 586 kw['chains'] = self.chainEntry.get() 587 kw['res'] = self.resEntry.get() 588 kw['atoms'] = self.atomEntry.get() 589 return kw
590 591
592 - def get(self, event=None, withMsg=False):
593 args = self.buildArgs() 594 atArg = self.atomEntry.get() 595 #print "atArg=", atArg, "=='' is", atArg=="" 596 resArg = self.resEntry.get() 597 #print "resArg=", resArg, "=='' is", resArg=="" 598 chainArg = self.chainEntry.get() 599 #print "chainArg=", chainArg, "=='' is", chainArg=="" 600 molArg = self.molEntry.get() 601 #print "molArg=", molArg, "=='' is", molArg=="" 602 if atArg!="": 603 args = molArg + ':' + chainArg + ':' + resArg + ':' + atArg 604 elif resArg!="": 605 args = molArg + ':' + chainArg + ':' + resArg 606 elif chainArg!="": 607 args = molArg + ':' + chainArg 608 else: 609 args = molArg 610 #print "calling StringSelector.select with args=", args 611 selitem, msgStr = StringSelector().select(self.molSet, args, sets=self.sets, caseSensitive=self.userPref) 612 #if StringSelector starts returning msgs fix here 613 #selitem, msgStr = StringSelector().select(self.molSet, args, self.userPref) 614 #return selitem, msgStr 615 #if selitem and len(selitem): 616 # print "returning len(selitem)=", len(selitem) 617 if withMsg: 618 return selitem, msgStr 619 else: 620 return selitem
621 622
623 - def set(self, val):
624 valList = string.split(val, ',') 625 if not len(valList)==4: 626 delta = 4-len(valList) 627 for i in range(delta): 628 valList.append('') 629 if valList[0]!="''": 630 self.molEntry.insert('end', valList[0]) 631 if valList[1]!="''": 632 self.chainEntry.insert('end', valList[1]) 633 if valList[2]!="''": 634 self.resEntry.insert('end', valList[2]) 635 if valList[3]!="''": 636 self.atomEntry.insert('end', valList[3])
637 638
639 - def Show_cb(self, event=None):
640 if self.showCurrent.get(): 641 current = self.get() 642 #if stringSelector starts returning msgs fix here 643 #current, msgStr = self.get() 644 if not self.vf: 645 print 'nowhere to show' 646 return 647 if not current: 648 msg = 'nothing to show' 649 self.vf.warningMsg(msg, title='String Selector GUI WARNING:') 650 self.showCurrent.set(0) 651 return 652 allAt = current.findType(Atom) 653 if allAt is None or len(allAt)==0: 654 self.showCross.Set(vertices=[], tagModified=False) 655 else: 656 self.showCross.Set(vertices = allAt.coords, tagModified=False) 657 else: 658 self.showCross.Set(vertices=[], tagModified=False) 659 self.vf.GUI.VIEWER.Redraw()
660 661
662 - def Clear_cb(self, event=None):
663 for item in [self.molEntry, self.chainEntry, self.resEntry, \ 664 self.atomEntry]: 665 item.delete(0, 'end') 666 #also have to turn off all the vars and reset all Cts 667 ss1 = ['molVar', 'chainVar', 'ResSetsVar', \ 668 'AtomSetsVar'] 669 dTkList = [] 670 for item in ss1: 671 if hasattr(self, item): 672 #dTkList.append(eval('self.'+item)) 673 dTkList.append(getattr(self, item)) 674 #dTkList = [self.molVar, self.chainVar, self.ResSetsVar, \ 675 #self.AtomSetsVar] 676 for d in dTkList: 677 for k in d.keys(): 678 d[k].set(0) 679 ss2 = ['oldmolVar', 'oldchainVar', 'oldResSetsVar',\ 680 'oldAtomSetsVar'] 681 dintList = [self.molCts, self.chainCts] 682 for item in ss2: 683 if hasattr(self, item): 684 #dintList.append(eval('self.'+item)) 685 dintList.append(getattr(self, item)) 686 #dintList = [self.oldmolVar, self.oldchainVar, self.oldResSetsVar,\ 687 #self.oldAtomSetsVar] 688 for d in dintList: 689 for k in d.keys(): 690 d[k]=0 691 if self.showCurrent.get(): 692 self.showCurrent.set(0) 693 if self.showCross: 694 self.showCross.Set(vertices=[], tagModified=False) 695 self.vf.GUI.VIEWER.Redraw()
696 697
698 - def Close_cb(self, event=None):
699 if self.showCross: 700 self.showCross.Set(vertices=[], tagModified=False) 701 self.vf.GUI.VIEWER.Redraw() 702 self.master.withdraw()
703 704 705 #def buildFileMenu(self, event=None): 706 # cmdList = [self.Close_cb] 707 # #cmdList = [self.Accept_cb, self.Close_cb] 708 # #fileOpts = ['Accept', 'Close'] 709 # fileOpts = ['Close'] 710 # if not self.showFileMenu.get(): 711 # if not hasattr(self.fileMB, 'menu'): 712 # self.buildMenu(fileOpts, self.fileMB, None, None, 713 # type = 'button', cmdList = cmdList) 714 # self.showFileMenu.set(1) 715 # else: 716 # self.fileMB.menu.unpost() 717 # self.showFileMenu.set(0) 718 719 720 #def buildHelpMenu(self, event=None): 721 #tkMessageBox.showwarning('Warning',self.helpMsg) 722 723
724 - def buildCmdDict(self):
725 self.cmdDict = {} 726 for key, value in self.optsDict.items(): 727 if value: 728 if type(value) == types.DictionaryType and \ 729 value.has_key('command'): 730 self.cmdDict[key] = value['command'] 731 elif key in self.ifdDict.keys() and self.ifdDict[key].has_key('command'): 732 self.cmdDict[key] = self.ifdDict[key]['command']
733 734
735 - def updateChildren(self):
736 d = self.optsDict 737 for key, chList in self.flexChildren.items(): 738 val = d[key] 739 if val: 740 #set alternate children list 741 for ch in chList: 742 d[ch] = val
743 744
745 - def buildFrame(self, widName, parent = None):
746 747 #print 'in buildFrame with ', widName 748 if parent==None: 749 parent = self 750 widDict = self.ifdDict[widName] 751 newWid = apply(Tkinter.Frame, (parent,), widDict['wcfg']) 752 exec('self.'+widName+'=newWid') 753 apply(newWid.pack, (), widDict['packCfg'])
754 755
756 - def buildGUI(self):
757 #first build Frames 758 #which set up Menubar and rest 759 #self.MBFrame = Tkinter.Frame(self.master, relief='raised') 760 #widStrings = ['MBFrame', 'WidFrame'] 761 self.buildFrame('topFrame' ) 762 self.buildFrame('buttonFrame' ) 763 widStrings = ['entryFrame', 'menuFrame'] 764 keyList = [self.entryKeywords, self.menuKeywords] 765 for num in range(2): 766 wid = widStrings[num] 767 widDict = self.ifdDict[wid] 768 if self.optsDict.get(wid): 769 for k, v in self.optsDict.get(wid).items(): 770 if type(v) == types.DictionaryType: 771 for subk, subv in v.items(): 772 widDict[k][subk] = subv 773 else: widDict[k] = v 774 newWid = apply(Tkinter.Frame, (self.topFrame,), widDict['wcfg']) 775 exec('self.'+wid+'=newWid') 776 apply(newWid.pack, (), widDict['packCfg']) 777 #if num==1: self.updateChildren() 778 self.buildWidgets(keyList[num], newWid) 779 self.buildWidgets(self.buttonKeywords, self.buttonFrame) 780 self.buildWidgets(self.masterbuttonKeywords, self.buttonFrame)
781 782
783 - def buildWidgets(self, keyList, master):
784 for key in keyList: 785 value = self.optsDict.get(key) 786 if not value: 787 #skip this widget 788 #print 'skipping ', key 789 continue 790 else: 791 #first check whether there is a cfg 792 widDict = self.ifdDict[key] 793 if value!=1: 794 #check whether it's a dictionary 795 for k, v in value.items(): 796 if type(v) == types.DictionaryType: 797 for subk, subv in v.items(): 798 widDict[k][subk] = subv 799 else: widDict[k] = v 800 #make the widget here 801 w = widDict['widgetType'] 802 del widDict['widgetType'] 803 804 #save possible pack info + command to bind 805 widCallback = widDict.get('callBack') 806 if widCallback: del widDict['callBack'] 807 packCfg = widDict.get('packCfg') 808 if packCfg: del widDict['packCfg'] 809 else: packCfg = self.packCfg 810 811 #save possible menuBuild cmds 812 buildMenuCmd = widDict.get('buildMenuCmd') 813 if buildMenuCmd: del widDict['buildMenuCmd'] 814 815 #if w==Tkinter.Menubutton: master = self.MBFrame 816 #else: master = self.WidFrame 817 newW = apply(w, (master,), widDict['wcfg']) 818 819 if buildMenuCmd: 820 newW.bind('<ButtonPress>', buildMenuCmd, add='+') 821 822 #frames have children to build and pack 823 if widDict.has_key('children'): 824 for ch in widDict['children']: 825 if not self.optsDict.get(ch): continue 826 else: 827 #first grab any passed info 828 wD =self.ifdDict[ch] 829 opD = self.optsDict[ch] 830 if type(opD)==types.DictionaryType: 831 for k, v in opD.items(): 832 wD[k] = v 833 # mark this ch as done 834 self.optsDict[ch] = 0 835 ch_w = wD['widgetType'] 836 del wD['widgetType'] 837 childCallback = wD.get('callBack') 838 if childCallback: del wD['callBack'] 839 childPackCfg = wD.get('packCfg') 840 if childPackCfg: del wD['packCfg'] 841 else: childPackCfg = self.packCfg 842 newChild = apply(ch_w, (newW,), wD['wcfg']) 843 newChild.pack(childPackCfg) 844 exec('self.'+ch+'= newChild') 845 if childCallback: 846 self.bindCallback(newChild, childCallback) 847 848 #if key=='spacing': 849 #newW.pack = newW.frame.pack 850 851 852 #if key in ['fileMB', 'cenMB', 'visiMB', 'helpMB']: 853 #newW.bind('<ButtonPress>', cmdDict[key], add='+') 854 855 newW.pack(packCfg) 856 857 exec('self.'+key+'= newW') 858 859 #check for a command to bind to newW 860 if key in self.cmdDict.keys(): 861 ##binding depends on type of widget 862 if newW.__class__==Tkinter.Scale: 863 newW.bind('<ButtonRelease>', widDict['command']) 864 865 if widCallback: 866 self.bindCallback(newW, widCallback)
867 868
869 - def bindCallback(self, wid, cmdDict):
870 if type(cmdDict)!=types.DictionaryType: 871 #this is a cheap way to set up defaults 872 cmdDict = {} 873 event = cmdDict.get('event') 874 if not event: event = '<ButtonPress>' 875 add = cmdDict.get('add') 876 if not add: add = '+' 877 command = cmdDict.get('command') 878 if not command: command = self.updateBox 879 wid.bind(event, command, add=add)
880 881
882 - def returnEntries(self):
883 #build list of requested options to return 884 returnList=[] 885 for item in self.dict.keys(): 886 if self.dict[item]: 887 returnList.append(self.ifdDict['item']) 888 return returnList
889 890 891 892 if __name__=='__main__': 893 #code to test it here 894 print 'testing stringSelector' 895 import pdb 896 897 root = Tkinter.Tk() 898 root.withdraw() 899 top = Tkinter.Toplevel() 900 top.title = 'StringSelectorGUI' 901 902 strSel = StringSelectorGUI(top, all = 1, crColor=(1.,0.,0.)) 903