[wxPython] VTK + wxPython flicker issue on Linux/GTK.

The wxVTKRenderWindow always comes up sized to 300x300 pixels. Once you resize the parent
window, the VTKwindow resizes almost correctly. I see a single column of pixels on the right hand side.
I've tried modifying the wxVTKRenderWindow code to fix some of this behavior, but I've been unsuccessful.
I've avoided the problem by making my initial parent window 300x300.

I'm using wxPython 2.2.1.

-Scott

···

-----Original Message-----
From: Prabhu Ramachandran [mailto:prabhu@aero.iitm.ernet.in]
Sent: Wednesday, March 07, 2001 2:03 PM
To: VTK users list; wxpython-users@lists.sourceforge.net
Subject: [wxPython] VTK + wxPython flicker issue on Linux/GTK.

hi,

The only thing holding me back from leaving Tkinter and starting to
use wxPython is that under Debian GNU/Linux 2.2 with
libwxgtk2.2-python version 2.2.1 and VTK 3.2beta when one runs the
wxVTKRenderWindow.py demo there is a huge amount of flicker when one
rotates the cone. There is also some very bad clipping that occurs if
one zooms into the cone. The initial VTK window is also not sized
properly. I know almost nothing about wxPython, so have no idea if
these issues have been fixed or what the exact problem is.

I'd appreciate if someone could tell me where the problem is and if
there is a fix. I am not subscribed on the wxPython-users list so
please CC me in on any messages from there.

thanks,
prabhu

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

Are you using the updated wxVTKRenderWindow code from
http://www.prism.gatech.edu/~gte552m/wxVTKWindow.html? I just tried it with this code (actually slightly
modified) and could set my wxFrame to wxSize(150, 140) and wxSize(450, 340). I also could not get the
column of pixels problem on the right either. Perhaps we are talking about different wxVTK packages because
there are a few :(.

As for Prabhu's original flicker question, perhaps you could take a look at the wxVTKRenderwindow code
mentioned above and back-port it to python. I am not skilled in this, but from what I remember of the
wxPython-VTK code it was similar to the above c++ class wxVTKWindow.

Harris Scott R CIV AFRL/SNJM wrote:

···

The wxVTKRenderWindow always comes up sized to 300x300 pixels. Once you resize the parent
window, the VTKwindow resizes almost correctly. I see a single column of pixels on the right hand side.
I've tried modifying the wxVTKRenderWindow code to fix some of this behavior, but I've been unsuccessful.
I've avoided the problem by making my initial parent window 300x300.

I'm using wxPython 2.2.1.

-Scott

-----Original Message-----
From: Prabhu Ramachandran [mailto:prabhu@aero.iitm.ernet.in]
Sent: Wednesday, March 07, 2001 2:03 PM
To: VTK users list; wxpython-users@lists.sourceforge.net
Subject: [wxPython] VTK + wxPython flicker issue on Linux/GTK.

hi,

The only thing holding me back from leaving Tkinter and starting to
use wxPython is that under Debian GNU/Linux 2.2 with
libwxgtk2.2-python version 2.2.1 and VTK 3.2beta when one runs the
wxVTKRenderWindow.py demo there is a huge amount of flicker when one
rotates the cone. There is also some very bad clipping that occurs if
one zooms into the cone. The initial VTK window is also not sized
properly. I know almost nothing about wxPython, so have no idea if
these issues have been fixed or what the exact problem is.

I'd appreciate if someone could tell me where the problem is and if
there is a fix. I am not subscribed on the wxPython-users list so
please CC me in on any messages from there.

thanks,
prabhu

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

_______________________________________________
This is the private VTK discussion list.
Please keep messages on-topic. Check the FAQ at: <http://public.kitware.com/cgi-bin/vtkfaq&gt;
Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtkusers

--
David D. Marshall
ARTLab System Administrator/GRA
Georgia Institute of Technology, Atlanta Georgia, 30332
http://www.ae.gatech.edu/research/artlab/artl/artlab.html
mailto:gte552m@prism.gatech.edu
think: Why anonymity is good, http://www.cato.org/pubs/briefs/bp-054es.html

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

hi,

<snip>

    > As for Prabhu's original flicker question, perhaps you
    > could take a look at the wxVTKRenderwindow code mentioned
    > above and back-port it to python. I am not skilled in
    > this, but from what I remember of the wxPython-VTK code it
    > was similar to the above c++ class wxVTKWindow.

I just looked at your package. However, my knowledge of GTK and wx is
not good enough to help me here. Here is what i understand looking at
the vtk.py from wxPython and wxVTKWindow.cpp that ships with Brian's
and your wxVTK package.

     (1) You are right - both of them are pretty similar in what they
     try to do. One difference with wxPython.vtk is that there is no
     multiple inheritance and a renderwindow (not a
     renderwindowinteractor) is stored as an instance variable in the
     Python class. Would this make a big difference?

     (2) I noticed that you set the WindowId of the RenderWindow etc
     when doing an OnPaint. The vtk.py does this when the window is
     realized. There is another key difference on how the WindowId is
     set. The wxVTKWindow class does something funny for the
     __WXGTK__ case. You use a GtkPizza (I have no clue what that is)
     and set the RenderWindow's WindowId and DisplayID by looking at
     the pizza windows bin_window and getting its xwindow and
     xdisplay. The wxPython code cannot do something like this since
     it will not have access to Gtk/Gdk stuff. Instead of doing this
     the wxPython.vtk code merely does this:

   hdl = seld.GetHandle()
   self.renderWindow.SetWindowInfo(str(hdl))

     Does this matter? Or do you have to do it someother way?

So my questions are:

     (1) Does it matter that in wxPython we store

Rather than my poking at this stuff in a random fashion without
knowing what I am doing I'd appreciate it if someone who has some clue
of what is going on helps. I have tried

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

hi,

Sorry, the last mail was incomplete - I accidentally sent it out when
I didnt intend sending it at all! Terribly sorry.

    > As for Prabhu's original flicker question, perhaps you
    > could take a look at the wxVTKRenderwindow code mentioned
    > above and back-port it to python. I am not skilled in
    > this, but from what I remember of the wxPython-VTK code it
    > was similar to the above c++ class wxVTKWindow.

I just looked at your package. However, my knowledge of GTK and wx is
not good enough to help me here. Here is what i understand looking at
the vtk.py from wxPython and wxVTKWindow.cpp that ships with Brian's
and your wxVTK package.

     (1) You are right - both of them are pretty similar in what they
     try to do. One difference with wxPython.vtk is that there is no
     multiple inheritance and a renderwindow (not a
     renderwindowinteractor) is stored as an instance variable in the
     Python class. Would this make a big difference?

     (2) I noticed that you set the WindowId of the RenderWindow etc
     when doing an OnPaint. The vtk.py does this when the window is
     realized. There is another key difference on how the WindowId is
     set. The wxVTKWindow class does something funny for the
     __WXGTK__ case. You use a GtkPizza (I have no clue what that is)
     and set the RenderWindow's WindowId and DisplayID by looking at
     the pizza windows bin_window and getting its xwindow and
     xdisplay. The wxPython code cannot do something like this since
     it will not have access to Gtk/Gdk stuff. Instead of doing this
     the wxPython.vtk code merely does this:

         hdl = seld.GetHandle()
         self.renderWindow.SetWindowInfo(str(hdl))

     Does this matter? Or do you have to do it someother way?

So my questions are:

     (1) Does it matter that in wxPython we store a RenderWindow
     instead of a RenderWindowInteractor as you do?

     (2) Does it matter if you set the WindowId etc. in OnPaint or on
     the OnCreateWindow?

     (3) Do you have to do that GtkPizza thing to get things working
     with wxGtk? Is there another way? Can it be done under Python??

My guess is that item (3) is the most crucial. Rather than my poking
at this stuff in a random fashion without knowing what I am doing I'd
appreciate it if someone who has some clue of what is going on helps.

thanks a ton and sorry for the incomplete post that I made earlier.

regards,
prabhu

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

Prabhu Ramachandran wrote:

     (2) I noticed that you set the WindowId of the RenderWindow etc
     when doing an OnPaint. The vtk.py does this when the window is
     realized. There is another key difference on how the WindowId is
     set. The wxVTKWindow class does something funny for the
     __WXGTK__ case. You use a GtkPizza (I have no clue what that is)
     and set the RenderWindow's WindowId and DisplayID by looking at
     the pizza windows bin_window and getting its xwindow and
     xdisplay. The wxPython code cannot do something like this since
     it will not have access to Gtk/Gdk stuff. Instead of doing this
     the wxPython.vtk code merely does this:

I've also some personal experience to share: I tested either the
python.vtk and David's (hi David) class on Windows, Linux and IRIX. The
python class really shows flickering problems, while David's class does
not, at least last realease with wxGTK225, but I noticed two problems for
both the classes on IRIX:

1) both the classes doesn't work: they seem to have problems with the
depth buffer
2) a bad event dispatching: the window did not resize.

The first problem can be solved for David's class (last release too) by
substituiting the SetWindowID with a SetParentID, the second problem
solved on itself with wxGTK 2.2.5. Anyway, these problems showed only
under IRIX + wxGTK.

I also noticed a strange small problem under Windows when the render
window is added to a sizer: it seems to resize to a wrong dimensions. I
solved with a

#ifdef __WXMSW__
  UpdateSize(event.GetSize().GetWidth()-16,
event.GetSize().GetHeight()-64);
#else
  UpdateSize(event.GetSize().GetWidth(), event.GetSize().GetHeight());
#endif __WXMSW__

I don't know why, but this way it works.

         hdl = seld.GetHandle()
         self.renderWindow.SetWindowInfo(str(hdl))

     Does this matter? Or do you have to do it someother way?

I tryed to pass the GetHandle result to SetWindowID to avoid the Pizza
style code but it crashes the program...

     (1) Does it matter that in wxPython we store a RenderWindow
     instead of a RenderWindowInteractor as you do?

Before David gave me its more elegant class I took the python.vtk class
and merged its code (converted to C++) with the Brian's original code and
it worked. Simply, the final class did not take advantage of the
vtkInteractor classes code to manage the camera.

     (3) Do you have to do that GtkPizza thing to get things working
     with wxGtk? Is there another way? Can it be done under Python??

A good question would be, is there a way to make wxGTK returning the
windowid and displayId from gdk structures?

My personal question is: why not to include David'd class within the
wxWindow distribution, or to add it to wxPython's one in its original C++
version? I think the only problem would be we can not change bindings by
inheritance from this class (directly in python), but the vtk allows to
change the interaction style by setting a different vtkInteractorStyle
class to the renderwindow interactor, doesn't it?

Marco.

hi,

    > Are you using the updated wxVTKRenderWindow code from
    > http://www.prism.gatech.edu/~gte552m/wxVTKWindow.html? I
    > just tried it with this code (actually slightly modified)
    > and could set my wxFrame to wxSize(150, 140) and
    > wxSize(450, 340). I also could not get the column of
    > pixels problem on the right either. Perhaps we are talking
    > about different wxVTK packages because there are a few :(.

    > As for Prabhu's original flicker question, perhaps you
    > could take a look at the wxVTKRenderwindow code mentioned
    > above and back-port it to python. I am not skilled in
    > this, but from what I remember of the wxPython-VTK code it
    > was similar to the above c++ class wxVTKWindow.

Well, I just got the latest wxwindows version 2.2.5.1 and got it
compiled for debian. I got the sources from woody and managed to get
them compiled on my potato box. wxwindows seems to work okay. The
flicker problem with python is still there.

In order to test David's C++ wxVTKRenderWindow code I compiled the
stuff and ran both the Sample_gtk and the SplitSample_gtk test
programs without making any modifications to wxVTKWindow.cpp.
Unfortunately, both of them flicker quite a bit! :frowning: Even if I just
rotate the cone by a small amount, slowly, it is clear that there is
flicker.

None of my other VTK-Python code flickers like this. The standard
Cone.py example works, so does all my other code (and i have quite a
bit of it). I have a hardware accelerated card - so it is likely that
I am able to see the flicker. This also explaine Marco's experiences
of seeing flicker on the SGI machines.

I then looked at the VTK list archives and some of my saved messages
and found that Renaud KERIVEN <keriven@cermics.enpc.fr> mentioned that
replacing SetWindowId by SetParentId fixes the flicker problem. I did
just that and it fixed the problem with your C++ wxVTKWindow class. I
also tried removing the SetDisplayId part and that worked fine too.
Hence here are some modfications that I suggest for your class (this
is valid only for WXGTK since I cant test on any other platform):

      (1) You dont need to set the DisplayId. This can be safely
      removed.

      (2) Replace the SetWindowId to SetParentId - this removes all
      the flicker.

      (3) You shouldnt set the Parent Id each time you do OnPaint. if
      you do this then when I resize the programs window I get error
      messages from VTK like so:

        $ ./Sample_gtk
  ERROR: In vtkXRenderWindow.cxx, line 223
  vtkOpenGLRenderWindow (0x80cc5d0): ParentId is already set.

  ERROR: In vtkXRenderWindow.cxx, line 223
  vtkOpenGLRenderWindow (0x80cc5d0): ParentId is already set.

      etc. etc.
      
This should hopefully fix the flicker problems with the wxVTKWindow
class.

It appears to me that if a similar approach is taken with the Python
class the flicker with the Python code can also be eliminated. I
propose to add a member function to the vtkXRenderWindow and
vtkWin32OpenGLRenderWindow classes called SetParentInfo() which works
just like SetWindowInfo. Hopefully, by using this instead of the
SetWindowInfo the flicker can be completely eliminated. I will let
you folks know how it goes.

thanks,
prabhu

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

hi,

<snip>
    > A good question would be, is there a way to make wxGTK
    > returning the windowid and displayId from gdk structures?

This is an interesting point and might be useful in case we need to
embed other X applications into a wxGTK window. I thought about this
and poked around the X11 headers. The WindowId (variable is called
Window) itself looks like a unsigned int/long but the Display is a
little confusing. I looked at the relevant header - StringDefs.h but
couldnt figure out the type of a DisplayId. Could some wx or X guru
enlighten us here?

    > My personal question is: why not to include David'd class
    > within the wxWindow distribution, or to add it to
    > wxPython's one in its original C++ version? I think the
    > only problem would be we can not change bindings by
    > inheritance from this class (directly in python), but the
    > vtk allows to change the interaction style by setting a
    > different vtkInteractorStyle class to the renderwindow
    > interactor, doesn't it?

Problem with this approach is that by adding the wxVTKWindow class to
the wxWindow code base we will only add one huge build dependancy to
wxWindows and make it harder to build the whole thing. Hopefully, if
my other proposal to do this SetParentInfo thing works, we wont have
to bother anyone. :slight_smile:

prabhu

···

--
It is never wise to turn aside from knowing, however the knowing
comes.
  -- Merlinus Ambrosius, "The Hollow Hills", [Mary Stewart]

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

hi,

This is the promised update on the flicker issue with wxPython + VTK
on Linux with GTK.

In the earlier message I indicated that the C++ class wxVTKWindow
works fine when one changes the references to WindowId to ParentId.
As regards the Python code, the vtk.py shipped with wxPython sets the
WindowId by calling the wrapped function SetWindowInfo(char *) like
so:

        hdl = self.GetHandle()
        self.renderWindow.SetWindowInfo(str(hdl))

Since the flicker in the c++ class vanished when I used ParentId
instead of WindowId I proposed to add a new function called
SetParentInfo to the vtk classes. I just did that and recompiled the
libVTKGraphics.so and libVTKGraphicsPython.so. Then I changed the
wxPython/lib/vtk.py file to use this new function like so:

        hdl = self.GetHandle()
        self.renderWindow.SetParentInfo(str(hdl))

I am thrilled to say it now works like a charm! No more flicker!! I
also added an OnSize method to make the size change properly. I guess
when I get a little more familiar with wxPython I will send in an
improved wxVTKRenderWindow class.

I did notice a silly problem with all the wxPython demos. All of the
widgets do not seem to redraw on an expose event. Is this a known
bug??

Meanwhile, I will send the VTK patches to the VTK list separately and
hope that the stuff gets added to both the CVS tree and also the
3.2beta branch.

Now there is nothing stopping me from learning wxPython properly!!

Thanks for all the help and patient listening. :slight_smile:

regards,
prabhu

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

     (2) I noticed that you set the WindowId of the RenderWindow etc
     when doing an OnPaint. The vtk.py does this when the window is
     realized. There is another key difference on how the WindowId is
     set. The wxVTKWindow class does something funny for the
     __WXGTK__ case. You use a GtkPizza (I have no clue what that is)
     and set the RenderWindow's WindowId and DisplayID by looking at
     the pizza windows bin_window and getting its xwindow and
     xdisplay. The wxPython code cannot do something like this since
     it will not have access to Gtk/Gdk stuff. Instead of doing this
     the wxPython.vtk code merely does this:

         hdl = seld.GetHandle()
         self.renderWindow.SetWindowInfo(str(hdl))

     Does this matter? Or do you have to do it someother way?

wxPython remaps the GetHandle method on wxGTK to go through all the GtkPizza
stuff and return the actual X window handle instead of the gtk widget
pointer.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

In the earlier message I indicated that the C++ class wxVTKWindow
works fine when one changes the references to WindowId to ParentId.
As regards the Python code, the vtk.py shipped with wxPython sets the
WindowId by calling the wrapped function SetWindowInfo(char *) like
so:

        hdl = self.GetHandle()
        self.renderWindow.SetWindowInfo(str(hdl))

Since the flicker in the c++ class vanished when I used ParentId
instead of WindowId I proposed to add a new function called
SetParentInfo to the vtk classes. I just did that and recompiled the
libVTKGraphics.so and libVTKGraphicsPython.so. Then I changed the
wxPython/lib/vtk.py file to use this new function like so:

        hdl = self.GetHandle()
        self.renderWindow.SetParentInfo(str(hdl))

I am thrilled to say it now works like a charm! No more flicker!! I
also added an OnSize method to make the size change properly. I guess
when I get a little more familiar with wxPython I will send in an
improved wxVTKRenderWindow class.

This is good news!

I'll gladly take a patch when you are ready to do it. I did the original
VTK class for wxPython fairly blind, having never used VTK before and not
knowing much about it at all. I always wondered if I had done it right or
if there was a better way.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

I did notice a silly problem with all the wxPython demos. All of the
widgets do not seem to redraw on an expose event. Is this a known
bug??

Meanwhile, I will send the VTK patches to the VTK list separately and
hope that the stuff gets added to both the CVS tree and also the
3.2beta branch.

Now there is nothing stopping me from learning wxPython properly!!

Thanks for all the help and patient listening. :slight_smile:

regards,
prabhu

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

In over my head now. (not that I wasn't before).

I'm *trying* to build an instant_messaging-type client. Now that most of
the GUI is built, the fun begins.
I'm trying to figure out the most *appropriate* way to 1. start the GUI, 2.
simultaneously start the network,
3. update the GUI based on incoming (and outgoing) network events.

I'm guessing that, ideally, there should be minimal coupling of GUI and
networking modules, but even the first step of
initialization is akward for me. Should I create a Main class that calls
both the networking class and the wxPython mainGui Class as separate
threads,
or would it be better to create an instance of the networking class from
within the GUI's main interface class? (thinking that this would make
updating
variables easier and keep variable passing to a minimum)..

ANY ADVICE on this GREATLY APPRECIATED. for someone who's only been
programming for 5 months, this is quite a learning curve, and
I could use all the help I can get.

Thanks!!!!
Steve

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

A related question: wxWindows has this wxSocketServer class
    which allows you to handle events for network socket connections
    and socket I/O. Any chance of this being wrapped in wxPython
    in the near future?
    
    Y.

···

On Friday, March 09, 2001 at 11:59:08 PM, Steve A. wrote:

I'm trying to figure out the most *appropriate* way to 1. start the GUI, 2.
simultaneously start the network,
3. update the GUI based on incoming (and outgoing) network events.

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

Just wondering.... I could use a serious book about now.
If you want to reply to me, I'll keep confidence.

Best,
Clark

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

I can't figure out where I got this, but it was someone from this list.
Run it and telnet to port 8001.

Its a demo of asynchronous networking/GUI'ing. If it gets too
garbled, I can email it in a nicer format.

   Rob Cakebread

···

On Friday 09 March 2001 09:59 pm, you wrote:

In over my head now. (not that I wasn't before).

I'm *trying* to build an instant_messaging-type client. Now that most of
the GUI is built, the fun begins.
I'm trying to figure out the most *appropriate* way to 1. start the GUI, 2.
simultaneously start the network,
3. update the GUI based on incoming (and outgoing) network events.

#
# A demonstration of a wxPython GUI co-existing with threading and networking
#
import os
import string
import sys
import threading
import time
import socket
import asyncore
import asynchat
from wxPython.wx import *

SOCKET_PORT = 8001
BUFSIZE = 1024 # maximum size of a message read from the network

class MainFrame(wxFrame):

    ID_EXIT = 102

    def __init__(self, parent, ID, title):
        wxFrame.__init__(self, parent, ID,
                         title,
                         wxDefaultPosition, # position
                         wxSize(512,512))
        self.SetAutoLayout(true)
        self.CreateStatusBar()
        menuBar = wxMenuBar()
        menu = wxMenu()
        menu.AppendSeparator()
        menu.Append(self.ID_EXIT, "E&xit", "Terminate the program")
        menuBar.Append(menu, "&File");
        self.SetMenuBar(menuBar)
        EVT_MENU(self,self.ID_EXIT,self.TimeToQuit)

        sizer = wxBoxSizer(wxVERTICAL)
        self.SetSizer(sizer)

        # a logging window
        self.log = wxTextCtrl(self,-1,style = wxTE_MULTILINE|wxTE_READONLY)
        wxLog_SetActiveTarget(wxLogTextCtrl(self.log))
        sizer.Add(self.log,1,wxEXPAND|wxALL,1)

        # trap characters
        EVT_CHAR(self, self.OnChar)

        # start the thread
        self.network = NetworkThread(self)
        self.network.start()
        EVT_NETWORK(self,self.OnNetwork)

        # cleanup
        EVT_CLOSE(self, self.OnCloseWindow)

        self.show_status("Try telnet'ing to this host on port %d." %
SOCKET_PORT)

    def OnCloseWindow(self, event):
        self.shutdown_network()
        self.Destroy()

    def OnChar(self, event):
        key = event.KeyCode()
        if key == ord('c'):
            wxLog_FlushActive()
        else:
            event.Skip()
            return

    def TimeToQuit(self, event):
        self.Close(true)

    def OnNetwork(self, evt):
        wxLogMessage("Received: \"%s\"." % evt.msg)

    def shutdown_network(self):
        wxLogMessage('Shutting down server.')
        # wait for network thread to die
        if self.network.is_running():
            self.network.stop()
        nd = 1
        while self.network.is_running():
            self.show_status("Shutting down network service" + nd*'.')
            time.sleep(0.3)
            nd = nd + 1

    def show_status(self,t):
        self.SetStatusText(t)

class NetworkServer (asyncore.dispatcher):

    def __init__ (self,port,handler=None):
        self.port = port
        asyncore.dispatcher.__init__ (self)
        self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
        self.bind(('', port))
        self.listen(1) # max of one connection at a time
        self.handler = handler

    def handle_accept (self):
        conn, addr = self.accept()
        wxLogMessage ('Incoming connection from %s:%d' % (addr[0], addr[1]))
        NetworkChannel (conn, addr,self.handler)

class NetworkChannel (asynchat.async_chat):

    def __init__ (self, conn, addr,handler=None):
        asynchat.async_chat.__init__ (self, conn)
        self.set_terminator ('\r\n')
        self.in_buffer = ''
        self.handler = handler

    def handle_connect (self):
        self.send('\377\375\"')

    def collect_incoming_data (self, data):
        self.in_buffer = self.in_buffer + data

    def found_terminator (self):
        if self.handler:
            self.handler(self.in_buffer)
        else:
            print 'warning: unhandled message:',self.in_buffer
        self.in_buffer = ''

# Adapted from class event_loop in Sam Rushing's async package
class EventLoop:

    socket_map = asyncore.socket_map

    def __init__ (self):
        self.events = {}

    def go (self, timeout=5.0):
        events = self.events
        while self.socket_map:
            now = int(time.time())
            for k,v in events.items():
                if now >= k:
                    v (self, now)
                    del events[k]
            asyncore.poll (timeout)

    def schedule (self, delta, callback):
        now = int (time.time())
        self.events[now + delta] = callback

    def unschedule (self, callback, all=1):
        "unschedule a callback"
        for k,v in self.events:
            if v is callback:
                del self.events[k]
                if not all:
                    break

#
# the network thread communicates back to the main GUI thread via
# this sythetic event
#
class NetworkEvent(wxPyEvent):
    def __init__(self,msg=""):
        wxPyEvent.__init__(self)
        self.SetEventType(wxEVT_NETWORK)
        self.msg = msg
wxEVT_NETWORK = 2000
def EVT_NETWORK(win, func):
    win.Connect(-1, -1, wxEVT_NETWORK, func)

#
# This thread runs in the background receiving commands via
# a socket, then sends the command back to the GUI as a "NetworkEvent"
#
class NetworkThread(threading.Thread):

    def __init__(self,win):
        threading.Thread.__init__(self)
        self.win = win
        self.keep_going = true
        self.running = false
        self.server = NetworkServer(SOCKET_PORT,self.received_a_line)
        self.event_loop = EventLoop()

    def is_running(self):
        return self.running

    def stop(self):
        self.keep_going = 0

    def check_status(self,el,time):
        if not self.keep_going:
            asyncore.close_all()
        else:
            self.event_loop.schedule(1,self.check_status)
        
    def received_a_line(self,m):
        self.send_event(m)

    def run(self):
        self.running = true
        self.event_loop.schedule(1,self.check_status)
        # loop here checking every 0.5 seconds for shutdowns etc..
        self.event_loop.go(0.5)
        # server has shutdown
        self.send_event("Closed down network")
        time.sleep(1)
        self.running = false

    # send a synthetic event back to our GUI thread
    def send_event(self,m):
        evt = NetworkEvent(m)
        wxPostEvent(self.win,evt)
        del evt

class MyApp(wxApp):

    def OnInit(self):
        self.frame = frame = MainFrame(NULL, -1,
"wxPython+threading+asynchat")
        self.SetTopWindow(frame)
        frame.Show(true)
        EVT_CHAR(self, self.OnChar)
        return true
    def OnChar(self, event):
        key = event.KeyCode()
        if key == 27:
            self.shutdown()
        else:
            event.Skip()
            return
    def shutdown(self):
        self.frame.Close(true)

if __name__=='__main__':
    app = MyApp(0)
    app.MainLoop()
     
_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

Python GUI co-existing with threading and networking

Yes, that script will be a huge help! thank you for sharing it. It will probably take me a while to figure out what is happening (and how it's happening) though.

best,
Steve

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

Just wondering.... I could use a serious book about now.
If you want to reply to me, I'll keep confidence.

Yep. It's still in VERY early stages, although it's finally moving forward
again.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

I posted it a while back for possible inclusion in the "Snippets and
Samples" section of www.wxpython.org which is still under construction.

-Drew

[mailto:wxpython-users-admin@lists.sourceforge.net]On Behalf Of Rob
Cakebread

···

-----Original Message-----
From: wxpython-users-admin@lists.sourceforge.net
Sent: Sunday, March 11, 2001 5:50 PM
To: wxpython-users@lists.sourceforge.net
Subject: Re: [wxPython] Multithreading, Network programming, and
wxPython

........deleted

I can't figure out where I got this, but it was someone from this list.
Run it and telnet to port 8001.

Its a demo of asynchronous networking/GUI'ing. If it gets too
garbled, I can email it in a nicer format.

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

Hey, I just want to say thanks for posting that script. I'm now trying to make a sincere effort at understanding just what is going on and how to apply it to my own app, but it's not easy.

All I'm really wanting to do right now is to have a client/server program on one machine send the same client/server program on another machine instant-messages, for the second machine to receive those messages and display them- (using asyncore, asyn_chat, multithreading, and of course wxPython).

simple, so it would *seem*. If you have any suggestions or other snippets of example code that might make my life easier in trying to figure out how to do this, I would be nothing less than EXTREMELY GRATEFUL and even perhaps willing to compensate you with $$. *L* (haven't reached point of giving up yet).

again, thanks. without the generosity of the people on this mailing list, I would never have learned how enjoyable programming can be.

-Stephen

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

I would run a copy of that wxPython demo script on each side. Since
it works with telnet, find a simple line by line script that will send your
text to the demo script and your friends copy. I don't know of any
full-fledged telnets in Python, but I would look at one of the MUD
clients out there (Multi User Domain), and then incorporate that
code into the wxPython async demo.

  There's Lyntin and mcl:

  http://lyntin.sourceforge.net
  http://www.andreasen.org/mcl/

     Rob

···

On Tuesday 13 March 2001 11:30 pm, you wrote:

Hey, I just want to say thanks for posting that script. I'm now trying to
make a sincere effort at understanding just what is going on and how to
apply it to my own app, but it's not easy.

All I'm really wanting to do right now is to have a client/server program
on one machine send the same client/server program on another machine
instant-messages, for the second machine to receive those messages and
display them- (using asyncore, asyn_chat, multithreading, and of course
wxPython).

simple, so it would *seem*. If you have any suggestions or other snippets
of example code that might make my life easier in trying to figure out how
to do this, I would be nothing less than EXTREMELY GRATEFUL and even
perhaps willing to compensate you with $$. *L* (haven't reached point of
giving up yet).

again, thanks. without the generosity of the people on this mailing list,
I would never have learned how enjoyable programming can be.

-Stephen

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

Rob,

thanks for the suggestions and the links to the MUD clients.
I haven't yet pulled down the code for those clients because I've been working with the code of a simple telnet client I found in the Python Annotated Archives book. Unfortunately, the server you posted will recognize a connection, but it won't print out text as its entered (not a problem with the basic Unix telnet client I've been using.).'

So I'm thinking that, for me, before getting into the more complex stuff, I would just like to figure out how, using a client written in python, to get my text on the screen of this server. Below is the code for a *very* simple client that I've been using- if you or anyone else has suggestions about how to get the server to display the text that is being sent it would help a lot.

back to tinkering.
thanks!
Steve

code:

#!/usr/bin/env python

## a simple script for setting up a tcp/ip client

from socket import *

HOST = "192.168.1.111" #where server is located on my lan
PORT = 8001
ADDR = (HOST,PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)

while 1:
    data = raw_input('> ')
    if not data: break
    tcpCliSock.send(data)
tcpCliSock.close()

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users