Thread: loops in gui

    #1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    loops in gui


    Can anyone show me an example of wxPython with a while loop in it? cause i have been tring to add a while loop in one of my programs using wxPython.. but it stops the gui from loading.... i tried using threading but every time i am about to read about it.. i get disturbed by something or someone
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Where or why are you trying to use a while loop?

    Generally, with GUI programs, they are event-driven, so you respond to button clicks, mouse movements, etc. If you put a while loop in one of these methods, it wouldn't stop the GUI from loading - it would stop it from updating for a ... while.

    If you are trying to have something happen all the time while your GUI is running, make a method for the action, and set a timer to trigger the method every so often.

    In other words, a While loop is probably not the best solution to whatever you are trying to do in a GUI environment...
  4. #3
  5. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by sfb
    Where or why are you trying to use a while loop?

    Generally, with GUI programs, they are event-driven, so you respond to button clicks, mouse movements, etc. If you put a while loop in one of these methods, it wouldn't stop the GUI from loading - it would stop it from updating for a ... while.

    If you are trying to have something happen all the time while your GUI is running, make a method for the action, and set a timer to trigger the method every so often.

    In other words, a While loop is probably not the best solution to whatever you are trying to do in a GUI environment...
    Not nececeraly.. i was using socket.recv( ) in a while loop to get socket data.. didnt know what was going on cause it didnt want to load anything.... then i search around and i find

    Code:
    wxSocketClient::Connect
    
    bool Connect(wxSockAddress& address, bool wait = TRUE)
    
    Connects to a server using the specified address.
    
    If wait is TRUE, Connect will wait until the connection completes. Warning: This will block the GUI.
    I whent in to wxwidget irc chan and as you can see this class funtion ( wxSocketclient:Connect ) is the same as using the low level socket module... The only option i got is using threaded socket.. ( but dont know how yet, this is why i asked for a sample ) or select, or twisted.. And at the moment i dont want to learn twisted... so if anyone has an example using threading.. i would like to see it if its kool
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    This link should get you started:

    http://wiki.wxpython.org/index.cgi/LongRunningTasks

    Dave - The Developers' Coach
  8. #5
  9. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by DevCoach
    This link should get you started:

    http://wiki.wxpython.org/index.cgi/LongRunningTasks

    Dave - The Developers' Coach
    Ya im going to have to deal with this one then. thanx
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Forget the threading, looking at the link Dave supplied it looks like all you have to di is call the wxYield() function inside your loop to force the window to refesh and it should work fine..

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Nah you will just get an error about wxYeild being called recursively
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by xlordt
    Nah you will just get an error about wxYeild being called recursively
    You can get round this by calling wxGetApp().wxYield(True). Th global function wxGetApp returns the wxApp object, and the wxYield method takes a boolean parameter that makes it only yield when not called recursively.

    From the docs:

    wxApp::Yield
    bool Yield(bool onlyIfNeeded = FALSE)

    Yields control to pending messages in the windowing system. This can be useful, for example, when a time-consuming process writes to a text window. Without an occasional yield, the text window will not be updated properly, and on systems with cooperative multitasking, such as Windows 3.1 other processes will not respond.

    Caution should be exercised, however, since yielding may allow the user to perform actions which are not compatible with the current task. Disabling menu items or whole menus during processing can avoid unwanted reentrance of code: see ::wxSafeYield for a better function.

    Note that Yield() will not flush the message logs. This is intentional as calling Yield() is usually done to quickly update the screen and popping up a message box dialog may be undesirable. If you do wish to flush the log messages immediately (otherwise it will be done during the next idle loop iteration), call wxLog::FlushActive.

    Calling Yield() recursively is normally an error and an assert failure is raised in debug build if such situation is detected. However if the the onlyIfNeeded parameter is TRUE, the method will just silently return FALSE instead.
    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo