First I would break your irc script up into the three fundamental parts.
1: Connecting to the server
2: Sending Messages
3: Receiving messages
Part 1:
Add a button/menu or whatever you prefer to your gui that is for connecting to the server
- Bind your frame class to handle EVT_BUTTON to a event handler function that has the code for connecting to the server in it.
For Parts 2 and 3 look at the wiki artical that was mentioned in a previous reply and then see this.
Part 2:
As these event can take time you will probably want to handle them on a separate thread so that the gui doesn't freeze while waiting for a response, To send a message you will need to bind your input TextCtrl to the event EVT_TEXT_ENTER and then prepare/send the message in that handler by sending it off to a worker thread.
Part 3:
I would have the receive running in a loop a different worker thread, (instead of while True: you may want to use "select" instead as it is more efficient). Then when ever recieve returns data have it either post an event or call a processing function back on the main(gui) thread by using wx.CallAfter.
import socket
network = ’ irc.onlinegamesnet.net’
port = 6667
irc = socket.socket ( socket.AF_INET , socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
print irc.recv ( 4096 )
irc.send ( ‘NICK BlabBlot\r\n’ )
irc.send ( ‘USER BlabBlot BlabBlot BlabBlot :Python IRC\r\n’ )
while True:
data = irc.recv ( 4096 )
if data.find ( ‘PING’ ) != -1:
irc.send ( 'PONG ’ + data.split() [ 1 ] + ‘\r\n’ )
elif data.find ( ‘PRIVMSG’ ) != -1:
nick = data.split ( ‘!’ ) [ 0 ].replace ( ‘:’, ‘’ )
message = ‘:’.join ( data.split ( ‘:’ ) [ 2: ] )
print nick + ‘:’, message
irc.sendall ( ‘JOIN #asdasdfdgfdhj\r\n’ )
print dataI now want to integrate the script in the gui. In Example display “print data” in the chat textctrl. Can you help?
To put the text in the text control you will need to self.chat.AppendText(mymessage) instead of “print” for it to show in the control.
And another Question: Is there a possibility to add a background image to a textctrl?
Not sure but if it is, its probably only available on gtk
But for the most simple ‘get it working now’ version see the attachment, and start it from the command line so that you can see any (error)messages that may be printed to the console then click on the Connect button to start your recv loop, I couldn’t get your network code to connect to the given server so there may be some errors printed to the console in this case.
gui.py (4.12 KB)