<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Nov 3, 2010, at 8:22 AM, Elisabeth Ortega wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Dear all,<br><br>I'm trying to do a script that shows in a table an array of numerical<br>values.<br>One little script that shows what I'm trying to do is:<br><br>from chimera import runCommand as run<br><br>run("sel")<br>b=[]<br>for a in chimera.selection.currentAtoms():<br> b=a.serialNumber<br><br>from CGLtk.Table import SortableTable<br><br>t=SortableTable(b)<br>t.addColumn("serial", "Serial", format=None)<br>t.setData(b)<br>t.launch()<br><br>With this little script I'm trying to show in a column of the table the<br>serial number of the atoms of my system.</div></blockquote><div><br></div>Hi Elisabeth,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>You've done a lot of things right here, but a few important things wrong. One is that a Table can't be created in isolation; it has to be part of a window. The simplest way to create a window is to use the ModelessDialog class from chimera.baseDialog, like this:</div><div><br></div><div>from chimera.baseDialog import ModelessDialog</div><div>class MyDialog(ModelessDialog):</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>def fillInUI(self, parent):</div><div><span class="Apple-tab-span" style="white-space:pre"> </span># code to create table in here...</div><div><br></div><div>MyDialog()</div><div><br></div><div>[FYI, it's a "modeless" dialog because the user can interact with other parts of Chimera while the dialog is shown (as opposed to a "modal" dialog where the user must respond to it before doing anything else in Chimera)]</div><div><br></div><div>The second problem in your code is that the first argument to SortableTable should be the window or frame that it is to be placed in. The "parent" argument to the fillInUI method above is the frame in the dialog window that widgets (like your SortableTable) should be placed in. So the code to create the table is:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>t = SortableTable(parent)</div><div><br></div><div>The third problem with the code is that the argument to the setData method should be a list of some sort but your code does a "b=a.serialNumber" which makes b an integer, not a list. Another problem is that the arguments to addColumn are the column title and how to get the data value from an item, the latter of which should either be an attribute name or a function that takes an item as its argument. Since you are supplying a list of integers, the addColumn call would be:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>t.addColumn("Serial", int, format="%d")</div><div><br></div><div>or:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>t.addColumn("Serial", str)</div><div><br></div><div>Both 'int' and 'str' are Python built-in functions for conversion to integer or string.</div><div><br></div><div>The final problem with the code is that you need to call the grid() method on the table so that it gets shown (there are row/column arguments to grid() for positioning widgets relative to each other in a single frame, but since you only have one widget [the table] you can call grid() with no arguments).</div><div><br></div><div>The last thing, which isn't an error per se, is that I think it would be better if the data given to the table were a list of atoms, rather than a list of atom serial numbers. That way the table could display additional columns of information (such as atom name) rather than being restricted to just showing serial numbers.</div><div><br><div>Here's what I think your code should look like:</div><div><br></div><div>import chimera</div><div><div>from chimera.baseDialog import ModelessDialog</div><div>class TableDialog(ModelessDialog):</div><div><span class="Apple-tab-span" style="white-space: pre; "> </span>def fillInUI(self, parent):</div><div><span class="Apple-tab-span" style="white-space: pre; "> </span>from CGLtk.Table import SortableTable</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>t = SortableTable(parent)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span># str(atom) will return something like "ALA 59 A", so use str() func...</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>t.addColumn("Name", str)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>t.addColumn("Serial", "serialNumber", format="%d")</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>from chimera import runCommand as run</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>run("sel")</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>t.setData(chimera.selection.currentAtoms())</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>t.launch()</div><div><span class="Apple-tab-span" style="white-space:pre"> </span># the 'sticky' arg controls resize behavior,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span># with "nsew" the table will enlarge (or shrink)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span># as the dialog is resized</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>t.grid(sticky="nsew")</div><div><br></div><div>TableDialog()</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Finally, there is more detail about how ModelessDialogs work in the "Extension-Specific User Interface" programmer's example at <a href="http://www.cgl.ucsf.edu/chimera/docs/ProgrammersGuide/Examples/index.html">Example FrameSet</a></div><div><br></div><div>--Eric</div><div><br></div></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 16px; "><font face="Helvetica" size="5" style="font: normal normal normal 16px/normal Helvetica; "> Eric Pettersen</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 16px; "><font face="Helvetica" size="5" style="font: normal normal normal 16px/normal Helvetica; "> UCSF Computer Graphics Lab</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 16px; "><font face="Helvetica" size="5" style="font: normal normal normal 16px/normal Helvetica; "> <a href="http://www.cgl.ucsf.edu">http://www.cgl.ucsf.edu</a></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 16px; "><font face="Helvetica" size="5" style="font: normal normal normal 16px/normal Helvetica; "><br></font></div><blockquote type="cite"><div>But when I execute that, I got a<br>message in the Python Shell that says:<br><br>Traceback (most recent call last):<br> File "/home/eortega/intento-tabla.py", line 11, in <module><br> t=SortableTable(b)<br> File "CHIMERA/share/CGLtk/Table.py", line 489, in __init__<br> File "CHIMERA/lib/python2.5/lib-tk/Tkinter.py", line 2442, in __init__<br> Widget.__init__(self, master, 'frame', cnf, {}, extra)<br> File "CHIMERA/lib/python2.5/lib-tk/Tkinter.py", line 1923, in __init__<br> BaseWidget._setup(self, master, cnf)<br> File "CHIMERA/lib/python2.5/lib-tk/Tkinter.py", line 1901, in _setup<br> self.tk = master.tk<br>AttributeError: 'int' object has no attribute 'tk'<br><br>And I don't understand it. How can I build successfully my table?</div></blockquote></div><br><div apple-content-edited="true"><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; "><span class="Apple-style-span" style="font-size: medium;"><br></span><br class="Apple-interchange-newline"></div></span> </div><br></body></html>