Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Twisted and Tkinter

Reply
Thread Tools

Twisted and Tkinter

 
 
Chris
Guest
Posts: n/a
 
      04-27-2006
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()

 
Reply With Quote
 
 
 
 
jordan.taylor2@gmail.com
Guest
Posts: n/a
 
      04-27-2006
Posting that error message would be helpful

 
Reply With Quote
 
 
 
 
Chris
Guest
Posts: n/a
 
      04-27-2006
Sorry. The error message is normally AttributeError: 'NoneType' object
has no attribute 'sendLine'"

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Posting that error message would be helpful


 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-27-2006
"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>



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      04-27-2006
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()

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-27-2006
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>



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      04-27-2006
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'

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-27-2006
"Chris" <(E-Mail Removed)> skrev i meddelandet news:(E-Mail Removed) 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>



 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      04-27-2006
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.

 
Reply With Quote
 
jordan.taylor2@gmail.com
Guest
Posts: n/a
 
      04-28-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [Twisted-Python] Twisted 13.0.0 released Glyph Python 0 04-03-2013 09:36 PM
Re: [Twisted-Python] Twisted 12.3.0 released Glyph Python 0 12-27-2012 06:40 AM
Re: [Twisted-Python] Re-working a synchronous iterator to use Twisted Jean-Paul Calderone Python 0 06-17-2008 10:14 PM
Can I set up a timed callback without Tkinter or twisted or something? Hendrik van Rooyen Python 6 10-17-2006 04:44 AM
twisted and tkinter chat client Chris Python 0 04-27-2006 06:42 PM



Advertisments