[Twisted-Python] gtkLineClient.py

Lee Harr missive at hotmail.com
Sat Aug 24 10:39:09 MDT 2002


# gtkLineClient.py
# Free. Please share

from twisted.protocols.basic import LineReceiver

from twisted.internet import gtkreactor
gtkreactor.install()

import gtk

from twisted.internet import reactor

class LoginInfo:
    def __init__(self):
        self.username = 'guest'
        self.host = 'localhost'
        self.port = 8888

        self.quit = 0

class LoginWindow(gtk.GtkWindow):
    def __init__(self, info):
        self.info = info

        w = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
        w.connect('destroy', self.quit)

        vb = gtk.GtkVBox()
        w.add(vb)

        b = gtk.GtkButton('QUIT')
        b.signal_connect('clicked', self.quit)
        vb.add(b)

        self.usernameEntry = gtk.GtkEntry()
        self.usernameEntry.set_text(self.info.username)
        self.usernameEntry.signal_connect('activate', self.login)
        vb.add(self.usernameEntry)

        b = gtk.GtkButton('LOGIN')
        b.signal_connect('clicked', self.login)
        vb.add(b)

        w.show_all()

        self.w = w

    def login(self, b):
        print 'LOGGING IN'
        self.info.username = self.usernameEntry.get_text()
        self.w.hide()
        gtk.mainquit()

    def quit(self, b):
        print 'QUITTING'
        self.info.quit = 1
        gtk.mainquit()

class Client(LineReceiver):
    def __init__(self, info):
        self.info = info

        w = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
        w.connect('destroy', self.quit)

        vb = gtk.GtkVBox()
        w.add(vb)

        q = gtk.GtkButton('QUIT')
        q.connect('clicked', self.quit)
        vb.add(q)

        c = gtk.GtkButton('CLOSE')
        c.connect('clicked', self.close)
        vb.add(c)

        s = gtk.GtkButton('SEND')
        s.connect('clicked', self.send)
        vb.add(s)

        self.w = w

    def connectionMade(self):
        self.w.show_all()
        print 'CONNECTED'
        self.sendLine('USERNAME_%s' % self.info.username)

    def send(self, b):
        self.sendLine('message')

    def lineReceived(self, line):
        print 'Server said:', line

    def closeConnection(self):
        self.transport.loseConnection()

    def close(self, b):
        print "CLOSING"
        self.sendLine('CLOSE')
        self.w.hide()
        self.closeConnection()
        reactor.stop()

    def quit(self, b):
        self.info.quit = 1
        self.close(b)

    def connectionLost(self):
        print 'Connection lost'
        self.w.hide()
        reactor.stop()

    def connectionFailed(self):
        print 'Connection failed'
        reactor.stop()

def main():
    info = LoginInfo()

    while 1:
        l = LoginWindow(info)
        gtk.mainloop()
        if l.info.quit:
            break

        e = Client(info)
        reactor.clientTCP(info.host, info.port, e)
        reactor.run()
        if info.quit:
            break

if __name__ == '__main__':
    main()


_________________________________________________________________
Chat with friends online, try MSN Messenger: http://messenger.msn.com





More information about the Twisted-Python mailing list