wxPython


November 7, 2006: 6:56 am: kpdPython, wxPython

After adding the LC_SORT_ASCENDING style to a list control I started to see some odd behavior. Some items would not drag-n-drop while other drag-n-drops dropped the wrong item! This is what happend:

Originally the list was unsorted. Since the data portion of items in a list control must be an integer (due to the underlying C++ wXWindows implementation), I had simply set the data portion to the index returned by InsertStringItem:


index = self.InsertStringItem( sys.maxint,  anObject.name )
self.SetItemData(index, index) 
self.dataMap[index] = anObject

Now this works fine as long as the ordering of the list control does not change. Naturally it does when you later go back and set the style to sorted! Once this style is set, you may receive the same index back multiple times from InsertStringItem. This sometimes overwrote entries in self.dataMap, causing the problems with drag-n-drop.

The solution was simple – when loading the list, keep your own index, incrementing it as items are added. Set the data for a given entry to this index and use this same index, not the one returned from InsertStringItem, to index the dataMap:


        self.dataMap = {}
        dataMapIndex=0
        for object in listOfObjectsToAdd:
              index = self.InsertStringItem( sys.maxint, object.name )
              self.SetItemData(index, itemDataMapIndex)
              self.dataMap[dataMapIndex]=( clip )
              dataMapIndex += 1

So now no matter how the list is reordered, the data associated with an entry will be the correct key into the dataMap.

I hope this tip may save others some time debugging, even though it is probably obvious to someone with wxPython/wxWindows experience.

February 25, 2006: 3:19 pm: kpdwxPython

When generating code for a given class, wxGlade will generate code corresponding to the first time it sees a given class name. This can lead to basically empty classes if you define your application frame first and it incorporates an application-specific window class. If you then later define the application-specific window class as an outer level panel – say for reuse, you will not get the generated python code. For example:

MyAppFrame
        panel in MyAppFrame of class CustomPanel
CustomPanel
        theButton

The python code generated for Custom panel will not contain code for ‘theButton.’ The solution is to move the definition of your custom classes ahead of any classes that reference them:
CustomPanel
      theButton
MyAppFrame
     panel in MyAppFrame of class CustomPanel

Would generate code for CustomPanel that has the ‘theButton‘ present as desired.

February 22, 2006: 6:32 am: kpdPython, wxPython

I was having trouble getting text to align correctly. Setting the background color of the item in question ( the label in this case ) to a different color than the dialog’s normal background helped identify the problem. I thought the item was not getting left-aligned correctly, but it turns out it was, the size of the column was just not what I expected. Any way, try changing colors to learn more about sizers.

changing background color of a sizer item

February 1, 2006: 6:22 pm: kpdPython, wxPython

Argh. I just realized today I had the *wrong* code posted in this example. The original post now contains the correct code.

January 19, 2006: 12:08 am: kpdPython, wxPython

I was having some trouble getting a window to resize when the contained controls were shown or hidden. After some searching and experimentation, I found this sequence to work:

where gui.xrc is:


<?xml version="1.0" encoding="cp1252"?>
<resource>
  <object class="wxFrame" name="FRAME1">
    <title>Test</title>
    <centered>1</centered>
    <object class="wxBoxSizer">
      <orient>wxVERTICAL</orient>
      <object class="sizeritem">
        <object class="wxToggleButton" name="showControls">
          <label>Show Controls</label>
          <checked>1</checked>
        </object>
      </object>
      <object class="sizeritem">
        <object class="wxPanel" name="controlPanel">
          <object class="wxBoxSizer">
            <orient>wxHORIZONTAL</orient>
            <object class="sizeritem">
              <object class="wxStaticText" name="theLabel">
                <label>Label:</label>
              </object>
            </object>
            <object class="sizeritem">
              <object class="wxTextCtrl" name="theTextControl"/>
              <option>1</option>
            </object>
          </object>
        </object>
      </object>
    </object>
  </object>
</resource>

2/1 Update – had wrong app code in place

« Previous Page