problem outputting text to an wxTextcontrol from another class

Wow...I honestly don't know where to begin...

1. "from wxPython.wx import *" is depreciated and has been for over a
year. Use "import wx" and then do your calls like "wx.TextCtrl(...)"

2. Pass in "self" to the ZeonServer during instantiation and store it
as an attribute of the class. Then do the call something like this:

def __init__(self,window,host='localhost', port=2900):
  self.appwin=window
  self.appwin.text2.WriteText("Starting server\n")

When you do this, make sure that when you construct your text control
that you assign it as an attribute of the class...ie
self.text2=wxTextCtrl(self, -1, "", size=(505, 290),
style=wxTE_MULTILINE)

3. Use wx.NewId() rather than statically assigning control ids to
things like ID_ABOUT,ID_EXIT, etc...

  If you are new to wxPython, I'd heartily suggest Robin's book
"wxPython In Action". Here's the amazon link...

Christoper L. Spencer
CTO ROXX, LLC
4515 Leslie Ave.
Cincinnati, OH
45242
TEL: 513-545-7057
EMAIL: chris@roxx.biz

···

On Sat, 26 Aug 2006 14:24:57 +0000 (UTC), Darklandz <darklandz.ssdd@gmail.com> wrote:

Hi,

I'm quite new to python and wxpython and i have a small problem with this code.

Operating system: Windows XP pro SP2
Python version: 2.4.3
wxpython 2.6

Program is a tcp server that i'm gonna develop further.
There are 2 classes, MyFrame that is the graphical interface for the server and
ZeonServer that is the class that handles the server.

The problem is that when the server get's started i want to get the messages the
server return printed in the wxtextcontrol that was created in the Myframe class.

I've been searching the web for a few days now and didn't find any help ...

here's the code (it runs but is far from complete)

from wxPython.wx import *
import thread, socket, sys

ID_ABOUT = 101
ID_EXIT = 102
ID_TEST = 103
ID_DEVNOTES = 104

class ZeonServer:

   mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   open_channels = {}
   BUFSIZE = 1024

   def __init__(self, host='localhost', port=2900):

       #MyFrame.text2.WriteText("Starting server\n")
       # the print statement below outputs it in a dos window but i want
       # it to be outputted to the text control in the Myapp class

       print 'Starting server...'
       try:
           self.mySocket.bind((host, port))
           self.mySocket.listen(5)
           print 'Zeon started successfully - Host %s - port %s' % (host, port)

       except:
           print 'Server failed to start'
           return

       while True:
           try:
               socket = self.mySocket.accept()
           except:
               print 'Error: failed to accept connection'
               continue

           channel, details = socket
           print 'Connected with ', details
           self.open_channels[details] = channel
           thread.start_new_thread(self.connect, (channel, details))

   def connect(self, channel, details):
       ip, port = details
       xmldoc = ''
       while True:
           try:
               buffer = channel.recv(self.BUFSIZE)
           except:
               self.disconnect(details)
               break

           if not buffer:
               self.disconnect(details)
           elif '\0' in buffer:
               xmldoc += buffer
               self.parse_cmd(details, xmldoc[:-1])
               xmldoc = ''
           else:
               xmldoc += buffer

   def parse_cmd(self, details, xmldoc):
       ip, port = details
       channel = self.open_channels[details]

       try:
           print "ontvangen : %s" %xmldoc
           self.broadcast("ok goed ontvangen")
          
       except:
           channel.send('<error>Invalid XML</error>\0')
           print "received invalid XML from %s" % details
           return

   def broadcast(self, str):
       print str
       for details in self.open_channels.keys():
           try:
               self.open_channels[details].send(str+'\0')
           except:
               self.disconnect(details)

   def disconnect(self, details):
       ip, port = details
       if details in self.open_channels:
           self.open_channels[details].close()
           del self.open_channels[details]
           formed_msg = "[%s:%s] closed the connection" % (str(ip), str(port))

class MyFrame(wxFrame):
   def __init__(self, parent, ID, title):
       wxFrame.__init__(self, parent, ID, title,
                        wxDefaultPosition, wxSize(512, 400))
       self.CreateStatusBar()
       self.SetStatusText("Server is OFFLINE !")

       menu = wxMenu()
       menu.Append(ID_TEST, "&Start server","Startup Zeon")
       menu.AppendSeparator()
       menu.Append(ID_EXIT, "E&xit", "Terminate the server")

       menu_about = wxMenu()
       menu_about.Append(ID_ABOUT, "&About","More information about this program")
       menu_about.AppendSeparator()
       menu_about.Append(ID_DEVNOTES, "Dev notes", "Developper notes")

       menuBar = wxMenuBar()
       menuBar.Append(menu, "&Server");
       menuBar.Append(menu_about, "&About");
       
       self.SetMenuBar(menuBar)
      
       EVT_MENU(self, ID_ABOUT, self.OnAbout)
       EVT_MENU(self, ID_EXIT, self.TimeToQuit)
       EVT_MENU(self, ID_TEST, self.StartServer)
       EVT_MENU(self, ID_DEVNOTES, self.TimeToQuit)

       text2 = wxTextCtrl(self, -1, "", size=(505, 290), style=wxTE_MULTILINE)
       text2.WriteText("Zeon v0.1 Beta, welcome ...\n")
       
   def OnAbout(self, event):
       dlg = wxMessageDialog(self,
       """Zeon Server

          Programmed by Darklandz
  
          v 0.1 BETA """,
       "About Me", wxOK | wxICON_INFORMATION)
       dlg.ShowModal()
       dlg.Destroy()

   def StartServer(self, event):
       new_server = ZeonServer()

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

class MyApp(wxApp):
   def OnInit(self):
       
       frame = MyFrame(None, -1, "Zeon Server")

       frame.Show(true)
       self.SetTopWindow(frame)
       return true

app = MyApp(0)
app.MainLoop()

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org