Twisted and Tkinter

Discussion in 'Python' started by Chris, Apr 27, 2006.

  1. Chris

    Chris Guest

    Does anyone know how to use twisted and tkinter. I have a simple tcp
    server
    and I want to send messages to it once connected using a tkinter
    button? I
    have built the code as far as I can but don't know what to do from
    here. Any reference I try to put to sendmessage in chatfactory doesn't
    seem to work, just brings up error messages.
    This is my code:

    from twisted.internet import reactor
    from twisted.internet.protocol import Protocol, ClientFactory
    from twisted.protocols.basic import LineReceiver
    from Tkinter import *
    from twisted.internet import tksupport

    class ChatClient(LineReceiver):

    def connectionMade(self):
    self.sendLine("Hello server")

    def lineReceived(self, line):
    pass

    def connectionLost(self, reason):
    pass



    class ChatFactory(ClientFactory):
    protocol = ChatClient

    def clientConnectionFailed(self, connector, reason):
    reactor.stop()

    def clientConnectionLost(self, connector, reason):
    reactor.stop()

    def sendMessage(self):
    self.sendLine("Test")


    root = Tk()
    b1 = Button(root,text="Send")
    #b1.configure(command=sendline to server here)
    b1.pack()
    tksupport.install(root)
    reactor.connectTCP('localhost',4567,ChatFactory())
    reactor.run()
     
    Chris, Apr 27, 2006
    #1
    1. Advertising

  2. Chris

    Guest

    Posting that error message would be helpful
     
    , Apr 27, 2006
    #2
    1. Advertising

  3. Chris

    Chris Guest

    Sorry. The error message is normally AttributeError: 'NoneType' object
    has no attribute 'sendLine'"

    wrote:
    > Posting that error message would be helpful
     
    Chris, Apr 27, 2006
    #3
  4. "Chris" wrot:

    > Sorry. The error message is normally AttributeError: 'NoneType' object
    > has no attribute 'sendLine'"


    please post the *entire* traceback, including the part that lists
    filenames, line numbers, and source code lines.

    </F>
     
    Fredrik Lundh, Apr 27, 2006
    #4
  5. Chris

    Chris Guest

    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
    TypeError: unbound method sendMessage() must be called with ChatFactory
    instance
    as first argument (got nothing instead)

    I have simplified the code as well, now attached below:

    from twisted.internet import reactor
    from twisted.internet.protocol import Protocol, ClientFactory
    from twisted.protocols.basic import LineReceiver
    from Tkinter import *
    from twisted.internet import tksupport

    class ChatClient(LineReceiver):

    def connectionMade(self):
    self.sendLine("Hello server")

    def lineReceived(self, line):
    print line

    def connectionLost(self, reason):
    pass


    class ChatFactory(ClientFactory):

    protocol = ChatClient

    def clientConnectionFailed(self, connector, reason):
    reactor.stop()

    def clientConnectionLost(self, connector, reason):
    reactor.stop()

    def sendMessage(self):
    self.sendLine("Test")

    root = Tk()
    b1 = Button(root,text="Send")
    b1.configure(command=ChatFactory.sendMessage)
    b1.pack()
    tksupport.install(root)

    reactor.connectTCP('localhost',8886,ChatFactory())
    reactor.run()
     
    Chris, Apr 27, 2006
    #5
  6. Chris wrote:

    > Exception in Tkinter callback
    > Traceback (most recent call last):
    > File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
    > return self.func(*args)
    > TypeError: unbound method sendMessage() must be called with ChatFactory
    > instance as first argument (got nothing instead)


    that's another error message, of course...
    >


    > class ChatFactory(ClientFactory):
    >
    > protocol = ChatClient
    >
    > def clientConnectionFailed(self, connector, reason):
    > reactor.stop()
    >
    > def clientConnectionLost(self, connector, reason):
    > reactor.stop()
    >
    > def sendMessage(self):
    > self.sendLine("Test")
    >
    > root = Tk()
    > b1 = Button(root,text="Send")
    > b1.configure(command=ChatFactory.sendMessage)


    umm. what is that line supposed to do ?

    if the purpose is to call the sendMessage method of the Chat-
    Message instance you're passing to connectTCP, it's probably
    better to create the instance first:

    chat = ChatFactory()

    root = Tk()
    b1 = Button(root,text="Send")
    b1.configure(command=chat.sendMessage)
    b1.pack()
    tksupport.install(root)

    reactor.connectTCP('localhost',8886,chat)
    reactor.run()

    </F>
     
    Fredrik Lundh, Apr 27, 2006
    #6
  7. Chris

    Chris Guest

    it now comes up with the error message

    Traceback (most recent call last):
    File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
    return self.func(*args)
    File "C:\Documents and Settings\chris\Desktop\Python\client.py", line
    30, in sendMessage
    self.sendLine("Test")
    AttributeError: ChatFactory instance has no attribute 'sendLine'
     
    Chris, Apr 27, 2006
    #7
  8. "Chris" <> skrev i meddelandet news:...
    > it now comes up with the error message
    >
    > Traceback (most recent call last):
    > File "C:\Python24\lib\lib-tk\Tkinter.py", line 1345, in __call__
    > return self.func(*args)
    > File "C:\Documents and Settings\chris\Desktop\Python\client.py", line
    > 30, in sendMessage
    > self.sendLine("Test")
    > AttributeError: ChatFactory instance has no attribute 'sendLine'


    Which is exactly what one would expect from this method

    def sendMessage(self):
    self.sendLine("Test")

    if the class you inherit from doesn't provide a sendLine method.

    But since you wrote that method, what did you expect it to do ?

    (If this is code from some Twisted manual or sample, it's probably time
    to double-check your code against the source...)

    </F>
     
    Fredrik Lundh, Apr 27, 2006
    #8
  9. Chris

    Chris Guest

    There is no manual that's the problem. The sendLine method is part of
    LineReceiver which is part of twisted. It's used to send a message over
    the transport link. I can get it working by overriding twisted's
    methods for example linereceived() or connectionmade(). But how do I
    get it to send a message over the transport to the server by clicking a
    tkinter button? There are no examples or documents on the internet that
    can help, I've searched for hours.
     
    Chris, Apr 27, 2006
    #9
  10. Chris

    Guest

    Fredrik is right, ChatFactory doesn't have sendLine as a method b/c it
    doesn't inherit it from ClientFactory. The code: protocol = ChatClient
    does do anything within ChatFactory except set the variable. Try out
    these.

    from twisted.protocols.basic import LineReceiver, LineReceiver.sendLine

    or change class ChatFactory(ClientFactory) --- to:

    class ChatFactory(ClientFactory, LineReceiver):

    or tell ChatFactory that sendLine comes from LineReceiver --

    class ChatFactory(ClientFactory):
    protocol = ChatClient

    def clientConnectionFailed(self, connector, reason):
    reactor.stop()

    def clientConnectionLost(self, connector, reason):
    reactor.stop()

    def sendMessage(self):
    self.LineReceiver.sendLine("Test")

    I'm pretty sure that those should work.
     
    , Apr 28, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    0
    Views:
    690
    Chris
    Apr 27, 2006
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    447
    Jean-Paul Calderone
    Jun 17, 2008
  3. Glyph
    Replies:
    0
    Views:
    177
    Glyph
    Dec 27, 2012
  4. Glyph
    Replies:
    0
    Views:
    155
    Glyph
    Apr 3, 2013
  5. Glyph
    Replies:
    0
    Views:
    51
    Glyph
    May 12, 2014
Loading...

Share This Page