0

Hi,

this function prints output of socket recv(). In my code, socket connects to the Samsung TV.

def connection_status(self):

        connection_status = sock.recv(64)
        label_connection_status = "Status: ", connection_status

        label_connection_status = Label(self.master, text= label_connection_status)
        label_connection_status.grid(row=7, column=3, columnspan=4, padx=(15,0), ipady=8, sticky=W)

The label gives me this:Screenshot of label

With print("Status: ", connection_status) i getting this: ('Status: ', '\x00\x0c\x00iapp.samsung\x04\x00d\x00\x01\x00')

And with print(label_connection_status) i getting this: .139719774023552

My questions are:

  1. How to format the ouput and why is output of print(label_connection_status) .139719774023552??

  2. I want to write "Status: Connected" in label if recv() returns something and "Status: Disconnected" if recv() returns "" (dont return anythink)

  3. I want to update the connection state with recv() every 1 second

Please help me! And sorry for my bad english. Thank you.

1 Answer 1

1

As for the second and third questions you could do something like: sock.settimeout(1)

try:
    data = sock.recv(64)
    print 'Status: Connected'
except socket.timeout:
    data = ''
    print 'Statuc: Disconnected'

In which you set the timeout to 1 second and if the connection times out it will print disconnected.

As for the label, the label is from Tkinter right? If I remember correctly that number represents your tkinter window (each window and children of the window get numbers to represent them to the operating system). I don't remember exactly how that works but I'm pretty sure that's what you got. If you want the text of the label you should use label_name["text"] or label_name.cget("text").

To answer your followup questions, here is the code, put this section in your init function

#Code to put in your __init__ function
self.connection_label = Label(self.master, text= '')
self.connection_label.grid(row=7, column=3, columnspan=4, padx=(15,0), ipady=8, sticky=W)
self.sock = socket.socket()
self.sock.connect(('127.0.0.1', 5555))
self.sock.settimeout(1)

(don't forget to change its ip and port) and here is the function you need:

def connection_status(self):
    try:
        data = self.sock.recv(64)
        self.connection_label['text'] = 'Status: Connected'
    except socket.timeout:
        data = ''
        self.connection_label['text'] = 'Status: Disconnected'
    #This makes the program call this every 1000 milliseconds
    self.master.after(1000, self.connection_status, self.master)

The line at the bottom makes the program call that function every x milliseconds which is in this case 1000 so 1 second. Also don't forger to actually call the function connection_status in your init function.

Sign up to request clarification or add additional context in comments.

17 Comments

Thanks for reply, but I getting this error: except socket.timeout: ^ SyntaxError: invalid syntax
I have it in def. And yes, the label is from Tkinter
And how to write the updated status to the Tk label?
I'm pretty sure the syntax error comes from the fact that I used python2 print command instead of print(), you should try that. As for the update the label, just do what you did, it should work.
No, its not from print(). I post it now to answers
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.