470,613 Members | 2,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,613 developers. It's quick & easy.

Twisted and Tkinter

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()

Apr 27 '06 #1
9 3260
Posting that error message would be helpful

Apr 27 '06 #2
Sorry. The error message is normally AttributeError: 'NoneType' object
has no attribute 'sendLine'"

jo************@gmail.com wrote:
Posting that error message would be helpful


Apr 27 '06 #3
"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>

Apr 27 '06 #4
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()

Apr 27 '06 #5
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>

Apr 27 '06 #6
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'

Apr 27 '06 #7
"Chris" <ce******@gmail.com> skrev i meddelandet news:11**********************@g10g2000cwb.googlegr oups.com...
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>

Apr 27 '06 #8
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.

Apr 27 '06 #9
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 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Paul Moore | last post: by
1 post views Thread by Fazer | last post: by
11 posts views Thread by mir nazim | last post: by
reply views Thread by Chris | last post: by
15 posts views Thread by Phillip B Oldham | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.