Question related to wx and dynamically updating windows

Almar Klein almar.klein at gmail.com
Mon Oct 20 16:59:36 CEST 2008


just to be sure,
did you try ".Update()"?

Almar

2008/10/20 Mike Driscoll <kyosohma at gmail.com>:
> On Oct 20, 5:43 am, Andy <a... at start.no> wrote:
>> I want to dynamically update a list of elements shown as a checkbox
>> list. A file is used to store the elements, and elements can be added
>> and deleted from the list. The trouble is that the window is not
>> properly updated after deleting/adding items.
>>
>> I use the Detach()/Destroy() to remove the sizer, and then I create a
>> new and updated sizer again. Apparently, not in a correct way...
>>
>> #################File createleftpanel.py############################
>> import wx
>> from gettext import gettext as _
>> from project import Project
>>
>> """
>> The class createLeftPanel
>> """
>>
>> class createLeftPanel(wx.Panel):
>>
>>      def __init__(self, parent):
>>          wx.Panel.__init__(self, parent)
>>
>>          #Initialization
>>          self.SetBackgroundColour('White')
>>          self.filename = 'tempfile.sim'
>>
>>          #Initiate file:
>>          initList = ['alfa', 'bravo', 'ekko']
>>          Project(self.filename).writeProject(initList)
>>          readList = Project(self.filename).readProject()
>>
>>          #Boxes
>>          self.mainbox = wx.BoxSizer(wx.VERTICAL)
>>          self.aSavedSizer = self.MakeSavedSizer(readList)
>>
>>          #Adding everything to mainbox
>>          self.mainbox.Add(self.aSavedSizer, 0, wx.RIGHT | wx.LEFT, 3)
>>
>>          self.mainbox.Fit(self)
>>          self.SetSizer(self.mainbox)
>>          self.Layout()
>>
>>      def OnDelete(self, event):
>>          #Deleting element from file
>>          for counter in range(0, len(self.elementList)):
>>              if self.elementList[counter][0].GetValue() == True:
>>                  Project(self.filename).removeLineFromProject(\
>>                      self.elementList[counter][2])
>>
>>          #Detaching and destroying the previous savedSizer
>>          self.mainbox.Detach(self.aSavedSizer)
>>          self.aSavedSizer.Destroy()
>>
>>          #Adding new aSavedSizer from file
>>          aSavedList = Project(self.filename).readProject()
>>          self.aSavedSizer = self.MakeSavedSizer(aSavedList)
>>          self.mainbox.Add(self.aSavedSizer, 0, wx.RIGHT | wx.LEFT, 3)
>>          self.mainbox.Fit(self)
>>          self.SetSizer(self.mainbox)
>>          self.Layout()
>>
>>      def OnAdd(self, event):
>>          #Adding element to file
>>          Project(self.filename).appendLineToProject(self.addText.GetValue())
>>
>>          #Detaching and destroying the previous savedSizer
>>          self.mainbox.Detach(self.aSavedSizer)
>>          self.aSavedSizer.Destroy()
>>
>>          #Adding new aSavedSizer from file
>>          aSavedList = Project(self.filename).readProject()
>>          self.aSavedSizer = self.MakeSavedSizer(aSavedList)
>>          self.mainbox.Add(self.aSavedSizer, 0, wx.RIGHT | wx.LEFT, 3)
>>          self.mainbox.Fit(self)
>>          self.SetSizer(self.mainbox)
>>          self.Layout()
>>
>>      def MakeSavedSizer(self, savedList):
>>
>>          #Putting saved items into static box together with checkbox for
>> deleting
>>          savedBox = wx.StaticBox(self, -1, _('Saved words'))
>>          savedSizer = wx.StaticBoxSizer(savedBox, wx.VERTICAL)
>>
>>          self.elementList = []
>>          for item in savedList:
>>              self.element = wx.CheckBox(self, -1, item,
>> pos=wx.DefaultPosition, \
>>                                    size=wx.DefaultSize)
>>              print item
>>              self.elementList.append((self.element,
>> self.element.GetId(), item))
>>              savedSizer.Add(self.element, 0, wx.ALL, 5)
>>
>>          #Delete button
>>          deleteBox = wx.BoxSizer(wx.HORIZONTAL)
>>          deleteText = wx.StaticText(self, -1, '', size=(125,-1))
>>          deleteButton = wx.Button(self, -1, _('Delete'))
>>          deleteBox.Add(deleteText, 0, wx.ALL, 1)
>>          deleteBox.Add(deleteButton, 0, wx.ALL, 1)
>>          self.Bind(wx.EVT_BUTTON, self.OnDelete, deleteButton)
>>
>>          #Add element + button
>>          addBox = wx.BoxSizer(wx.HORIZONTAL)
>>          self.addText = wx.TextCtrl(self, -1, value=_(''),  size = (125,
>> -1), \
>>                                           validator=wx.DefaultValidator)
>>          self.addText.SetMaxLength(12)
>>
>>          addButton = wx.Button(self, -1, _('Add'))
>>          addBox.Add(self.addText, 0, wx.ALL, 1)
>>          addBox.Add(addButton, 0, wx.ALL, 1)
>>          self.Bind(wx.EVT_BUTTON, self.OnAdd, addButton)
>>
>>          #Add to savedSizer
>>          savedSizer.Add(deleteBox, 0, wx.EXPAND, 0)
>>          savedSizer.Add(addBox, 0, wx.EXPAND, 0)
>>
>>          #self.SetSizer(savedSizer)
>>          self.Fit()
>>
>>          return savedSizer
>>
>> """ Testing routines for testing this module only"""
>> class Frame(wx.Frame):
>>      def __init__(self, parent):
>>          wx.Frame.__init__(self, parent=None, id=-1)
>>          self.panel = createLeftPanel(self)
>>          self.statusbar = self.CreateStatusBar()
>>
>> class App(wx.App):
>>      def OnInit(self):
>>          wx.App.__init__(self)
>>          self.frame = Frame(-1)
>>          self.frame.Show()
>>          self.SetTopWindow(self.frame)
>>          return True
>>
>>      def OnExit(self):
>>          pass
>>
>> if __name__ == '__main__':
>>      app = App()
>>      app.MainLoop()
>>
>> #################File project.py############################
>> import os
>>
>> """
>> The Project class handles reading/writing strings from/to file
>> """
>>
>> class Project(object):
>>      def __init__(self, filename):
>>          self.fname = filename
>>
>>      def readProject(self):
>>          fp = open(self.fname, 'r')
>>          flist = []
>>          for line in fp:
>>              flist.append(line.strip())
>>          fp.close()
>>          return flist
>>
>>      def writeProject(self, flist):
>>          fp = open(self.fname, 'w')
>>          for item in flist:
>>              fp.write('%s%s' % (item, os.linesep))
>>          fp.close()
>>
>>      def appendLineToProject(self, aString):
>>          #Check if aString is not in file already
>>          fp = open(self.fname, 'r+')
>>          beforeList = self.readProject()
>>          fp.close()
>>          if aString not in beforeList:
>>              fp = open(self.fname, 'a')
>>              fp.write('%s%s' % (aString, os.linesep))
>>              fp.close()
>>
>>      def removeLineFromProject(self, aString):
>>          #Remove all instances of aString
>>          fp = open(self.fname, 'r+')
>>          beforeList = self.readProject()
>>          afterList = []
>>          if aString in beforeList:
>>              for item in beforeList:
>>                  if aString.strip() != item.strip():
>>                      afterList.append(item)
>>                  else:
>>                      pass
>>          else:
>>              afterList = beforeList
>>
>>          self.writeProject(afterList)
>>          fp.close()
>
> I recommend re-posting to the wxPython User's List if you haven't
> already:
>
> http://wxpython.org/maillist.php
>
> Also, please make a small runnable sample: http://wiki.wxpython.org/MakingSampleApps
>
> That well help us (and you) figure out why it doesn't work
>
> Mike
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list