[wxPython] new to python/wxpython

This is the first language that I have really studied.
I've done basic in highschool and some c in college, i
can read SOME c but dont really understand a whole
lot. I am however VERY interesting in python and
especially wxpython. I would greatly appreciate your
help, I've been searching all over the net for
wxpython documentation and code. Thanks to everyone
who has been posting and housing wxpython info.

Here is my question, and I wouldnt ask this if it
hasnt stumped me for a day or two...I'm guessing no
one wants to see my whole code so i'll paraphrase:

The program consists of 3 pieces...2 wxframe instances
and 1 socket connection. Its mostly the example
socketclient that you see everywhere.

I have a MyFrame class that has one button and one
multi-line textCtrl, thats it. The button calls on a
second instance of WxFrame called DaFrame (original?),
in DaFrame I have 2 textCTRL boxes: host, port. This
second instance is to gather the info so I can give
the host/port to the socket class. I suppose I could
do a dialog instead of calling a new frame. (if there
is no other answer then that is what I will do but I
figure there's an answer so on with the problem)

My problem has 2 parts. First, when I get the info and
do a self.show(false) on DaFrame and pass host/port to
the socket class, DaFrame disappears but MyFrame
hangs. The socket class is in a loop, getting and
sending whatever is on the line. How can I avoid
hanging the window? Do I need to spawn a seperate
thread for the socket?

Here is my second part...If you remember there are
only two things on the MyFrame, the button that calls
DaFrame and the multi-line textctrl, the textCTRL is
for displaying what I receive from the socket
connection. Instead of having the socket called from
the second wxframe instance (daframe) how could I pass
the data I aquire (host/port) back to MyFrame (which
calls on DaFrame for the host/port info) and have the
socket class running in MyFrame?

I GREATLY appreciate any help, I think I'm going to
paste my code below...I dont know what the etiquette
is here, plz let me know if this is unacceptable. I
streamlined it, took out all menu stuff etc...

- ian

from wxPython.wx import *
import socket

# creating first instance of WxFrame
class MyFrame(wxFrame):
    def __init__(self, parent, ID, title):
        wxFrame.__init__(self, parent, ID, title,
                         wxDefaultPosition,
wxSize(800, 500))
        panel = wxPanel(self, -1)

        b = wxButton(panel, 77, "Connect to server",
wxPoint(10, 30), wxSize(110, 45))
        EVT_BUTTON(self, 77, self.OnClick)

# Here is the multi line that I want to display
received socket data
        multi1 = wxTextCtrl(panel, 30, "",
wxPoint(10,80),size=(600, 200), style=wxTE_MULTILINE)
      # multi1.write(data)

# This creates the scond instance of WxFrame so I can
gather connection info
    def OnClick(self, event):
        win = DaFrame(self, -1, "This is a wxFrame",
size=(300, 400),
                  style = wxDEFAULT_FRAME_STYLE)# |
wxFRAME_TOOL_WINDOW )
        win.Show(true)

···

#------------------------------------------------
       
class DaFrame(wxFrame):
    def __init__(self, parent, ID, title,
pos=wxDefaultPosition,
         size=wxDefaultSize,
style=wxDEFAULT_FRAME_STYLE):
        wxFrame.__init__(self, parent, ID, title, pos,
size, style)

        win = wxDialog(self, -1, "Make Connection",
wxDefaultPosition)
    # panel = wxPanel(win, -1)
     # wxWindow(win,
-1).SetBackgroundColour(wxNamedColour("WHITE"))
        self.host = 'localhost'
        host1 = wxStaticText(self, -1, "Host")
   # host2 = wxTextCtrl(self, 10, "rob.child.kin",
size=(125, -1))
        host2 = wxTextCtrl(self, 10, self.host,
size=(125, -1))
        host2.SetInsertionPoint(0)
        host = EVT_TEXT(self, 10, self.EvtTextHost)

        self.port = '4190'
        port1 = wxStaticText(self, -1, "Port")
        port2 = wxTextCtrl(self, 20, self.port,
size=(125, -1))
        port2.SetInsertionPoint(0)
        port = EVT_TEXT(self, 20, self.EvtTextPort)
    
        b1 = wxButton(self, 21, " OK ")
        b2 = wxButton(self, 22, " Cancel ")
        #b1 = wxButton(win, 21, " OK ",
wxPoint(150, 200), wxDefaultSize).SetDefault()
        #b2 = wxButton(win, 22, " Cancel ",
wxPoint(250, 200), wxDefaultSize)
        EVT_BUTTON(self, 21, self.OnOk)
        EVT_BUTTON(self, 22, self.TimeToQuit)

        sizer = wxFlexGridSizer(cols=2, hgap=6,
vgap=6)
        sizer.AddMany([host1, host2, port1, port2, b1,
b2])
            
        border = wxBoxSizer(wxVERTICAL)
        border.Add(sizer, 1, wxALL, 25)
        self.SetSizer(border)
        self.SetAutoLayout(true)

    def EvtTextHost(self, event):
        self.host = ''
        self.host = self.host+"%s\n" %
event.GetString()
        return self.host

    def EvtTextPort(self, event):
        self.port = ''
        self.port = self.port+"%s\n" %
event.GetString()
        return self.port
    
    def OnOk(self,event):
        self.Show(false)
        self.CallSock()

    def CallSock(self):
        portnum = string.atoi(self.port)
        s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
        s.connect((self.host, portnum))
        s.send('Hello, world')
        data = s.recv(1024)
        while data > 0:
            print 'Received', `data`
            data = s.recv(1024)
        s.close()
       
    def TimeToQuit(self, event):
        self.Close(true)

class MyApp(wxApp):
    def OnInit(self):
        frame = MyFrame(NULL, -1, "Hello from
wxPython")
        frame.Show(true)
        self.SetTopWindow(frame)
        return true

app = MyApp(0)
app.MainLoop()

__________________________________________________
Do You Yahoo!?
Find a job, post your resume.
http://careers.yahoo.com

I dont really have time to debug your app for you, but one debugging technique is to add debug output. Put a whole lot of 'prints' through the code with informative output. Run the code and see where its hanging...

eg.

    def CallSock(self):
        portnum = string.atoi(self.port)

  # you can do this with int. ie int(self.port)

  print "creating socket"

        s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)

  print "connecting socket"

        s.connect((self.host, portnum))

  print "sending message"

        s.send('Hello, world')

  print "receiving response"

        data = s.recv(1024)

  print "response received"

        while data > 0:
            print 'Received', `data`
            data = s.recv(1024)

  print "closing connection"

        s.close()

Whats the output say?

Kind Regards

Crispin

My problem has 2 parts. First, when I get the info and
do a self.show(false) on DaFrame and pass host/port to
the socket class, DaFrame disappears but MyFrame
hangs. The socket class is in a loop, getting and
sending whatever is on the line. How can I avoid
hanging the window? Do I need to spawn a seperate
thread for the socket?

That's one way. See
    http://wiki.wxpython.org/index.cgi/LongRunningTasks

···

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

look at the socket.select function - you can set timeouts ....
hope it help - regards

···

===========================================================
Heinl Raimund SSH - Software Systeme Heinl
gepr.Wirtschaftsinformatiker IHK Am Galling 3
Tel. 09151 / 7 10 99 91217 Hersbruck
Fax. 09151 / 90 50 51
http: coming soon
Email: heinlr@gmx.de

----- Original Message -----
From: "Robin Dunn" <robin@alldunn.com>
To: <wxpython-users@lists.wxwindows.org>
Sent: Friday, November 09, 2001 8:24 AM
Subject: Re: [wxPython] new to python/wxpython

>
> My problem has 2 parts. First, when I get the info and
> do a self.show(false) on DaFrame and pass host/port to
> the socket class, DaFrame disappears but MyFrame
> hangs. The socket class is in a loop, getting and
> sending whatever is on the line. How can I avoid
> hanging the window? Do I need to spawn a seperate
> thread for the socket?

That's one way. See
    http://wiki.wxpython.org/index.cgi/LongRunningTasks

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

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwindows.org
http://lists.wxwindows.org/mailman/listinfo/wxpython-users