Twisted and Tkinter

C

Chris

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

Chris

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

Fredrik Lundh

"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>
 
C

Chris

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

Fredrik Lundh

Chris said:
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>
 
C

Chris

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'
 
F

Fredrik Lundh

Chris said:
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>
 
C

Chris

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.
 
J

jordan.taylor2

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top