473,322 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 3408
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul Moore | last post by:
I hit a problem yesterday with my mail connection. In a desparate attempt to understand what was going on, I wanted to log the connection traffic. After a bit of searching, I found a post on c.l.p...
1
by: Fazer | last post by:
Hello, I am very interested in fooling around with Twisted Matrix's HTTP Web Server. I was thinking if .rpy scripts would run much faster than traditional CGI scripts? After looking how...
2
by: Mark Carter | last post by:
I'm trying to create a mail server in Twisted. I either get SMTPSenderRefused or SMTPException: SMTP AUTH extension not supported by server. What do I need to do to get it to work?
11
by: mir nazim | last post by:
hi, i m planning to start writing intranet applications and want ur real cool advices for choosing the correct platform. the choice is between the three: 1. Twisted 2. Medusa 3. Zope (i do...
2
by: Qp | last post by:
Is this even possible? I'm designing a simple chat and game client/server as an intro to Python, and it would be nice to represent the different interfaces (public chat room, private chat rooms,...
2
by: SeSe | last post by:
Hi, I am new to Twisted. I use a Twisted 1.3.0 on MS Windows XP Home Edition, my python version is 2.3 I try the TCP echoserv.py and echoclient.py example. But the client always fail with...
0
by: Chris | last post by:
Hi, Sorry for reposting but I changed my code and received a new error message so I thought I would try it on the group again. I have a working server and this is meant to be a chat client using...
6
by: Hendrik van Rooyen | last post by:
Hi, I want to do the equivalent of the after thingy in tkinter - setting up in effect a timed call back. My use case is as a "supervisory" timer - I want to set up an alarm, which I want to...
15
by: Phillip B Oldham | last post by:
Are there any python event driven frameworks other than twisted?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.