2 Replies - 1473 Views - Last Post: 07 May 2013 - 02:42 AM Rate Topic: -----

#1 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

wxPython ProgressDialog

Posted 06 May 2013 - 07:21 AM

Hi,

I'm trying to learn some wxPython but have some problems using a ProgressDialog and reading a text file into a ListBox. I googled around for some time and found a few examples but I still don't get what I want. So most of the code is what I found on the internet and i tried to adapt it to what I'm trying to achieve. This is where to original code comes from link
I just need a real example to understand what's going on.

Here the code that I was trying to use:

import wx

wx = wx
 
########################################################################
class MyForm(wx.Frame):
 
    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,
                          "ProgressDialog Tutorial")
        panel = wx.Panel(self, wx.ID_ANY)
        sizer = wx.BoxSizer(wx.VERTICAL)
        
        b = wx.Button(panel, label="Create and Show a ProgressDialog")
        b.Bind(wx.EVT_BUTTON, self.onButton)
        sizer.Add(b, wx.TOP|wx.CENTER, 5)

        
        self.lb1 = wx.ListBox(panel, -1, name='List Box')
        sizer.Add(self.lb1, wx.BOTTOM|wx.CENTER, 5)
        
        panel.SetSizer(sizer)
 
    #----------------------------------------------------------------------
    def onButton(self, evt):
        """
        Copied from the wxPython demo
        """
                
        max = 253
 
        dlg = wx.ProgressDialog("Progress dialog example",
                               "An informative message",
                               maximum = max,
                               parent=self,
                               style = wx.PD_CAN_ABORT
                                | wx.PD_APP_MODAL
                                #| wx.PD_ELAPSED_TIME
                                #| wx.PD_ESTIMATED_TIME
                                #| wx.PD_REMAINING_TIME
                                )
 
        keepGoing = True
        count = 0
        myData = []
 
        while keepGoing and count < max:
            with open('Stars.txt', 'r') as f:
                myData = f.readlines()
                self.lb1.Append(str(myData))
                listLength = len(myData)
            count += 1
            
            wx.MilliSleep(1)
                        
 
            if count < max:
                (keepGoing, skip) = dlg.Update(listLength, "Loading")
            else:
                (keepGoing, skip) = dlg.Update(listLength, "Finished loading data")
 
        dlg.Destroy()
 
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()




Basically i'm trying to achieve two things. First is to read a text file into a ListBox. While i get something in the listbox, it's not what is in the text file. I attached the text file to show what it is. So i don't see what i'm doing wrong there.
While reading the text file into the listbox I want to show a progressbar. Mi idea was to read the textfile into a list and then append this list to the listbox. Right now in the code I'm passing the variable listLength to the Update() method of the progressbar which is the list, although I think it should be the Append() funtion that i should pass so the progressbar is showing the progress correctly. With the current code the progress goes directly to the end while the listbox is still loading the text. I don't know however if the listbox has something like getlinecount or so. I think I need to get the line in the while loop and pass this to the Update(). Is this correct ? And if yes, how would I achieve this line count ?
The max value in the ProgressDialog is the number of lines in the text file. Any help would be great. I'm just trying to get the progressbar to show the correct progress by reading the text file and have the listbox show the correct values from the file.

Many thanks in advance.

Michael

Is This A Good Question/Topic? 0
  • +

Replies To: wxPython ProgressDialog

#2 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Re: wxPython ProgressDialog

Posted 06 May 2013 - 07:46 AM

OK, so i solved the issue with the lines
I added a for loop like this

while keepGoing and count < max:
            with open('Stars.txt', 'r') as f:
                myData = f.readlines()
            for line in myData:
                self.lb1.Append(line)



Now it's the progressbar that is left
Was This Post Helpful? 0
  • +
  • -

#3 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Re: wxPython ProgressDialog

Posted 07 May 2013 - 02:42 AM

I think i found the issue with the progressbar. I did the following now:
while keepGoing and count <= max:
            with open('Stars.txt', 'r') as f:
                myData = f.readlines()
            for line in myData:
                self.lb1.Append(line)
                amountofLines = self.lb1.GetCount()
                (keepGoing, skip) = dlg.Update(amountofLines, "Loading")



This looks like it's working (I think). However, when i close the app I get the following error:

Traceback (most recent call last):
File "F:\Python tests\ProgressDialog_Star_file.py", line 54, in onButton
(keepGoing, skip) = dlg.Update(amountofLines, "Loading")
File "C:\Python27\lib\site-packages\wx-2.9.4-msw\wx\_windows.py", line 3704, in Update
return _windows_.GenericProgressDialog_Update(*args, **kwargs)
wx._core.PyAssertionerror: C++ assertion "value <= m_maximum" failed at ..\..\src\generic\progdlgg.cpp(436) in wxGenericProgressDialog::Update(): invalid progress value

Can anybody help with this ? I think it is a difference between the value that is passed as a max value to the progressbar and the GetCount() but I don't seem able to figure it out.

Thanks in advance,
Michael
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1