Trying use radio buttons to select which host I'm referring to since ultimately there will only be two and they are fixed addresses.

This is the error: File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in call return self.func(args) File "Untitled 2.py", line 63, in command=lambda: callback_power_off(off, host)) File "Untitled 2.py", line 28, in callback_power_off connection.connect((host, port)) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(args) TypeError: coercing to Unicode: need string or buffer, instance found

from Tkinter import *
from socket import *
port = 7142

on = '02 00 00 00 00'
off = '02 01 00 00 00'




def callback_power_on(data, host):
    if not host:
        print "No host given!"
        return
    print "power on!"
    connection = socket(AF_INET, SOCK_STREAM)
    connection.connect((host, port))
    connection.sendall(add_checksum(data))
    connection.close()


def callback_power_off(data, host):
    if not host:
        print "No host given!"
        return
    print "power off!"
    connection = socket(AF_INET, SOCK_STREAM)
    connection.connect((host, port))
    connection.sendall(add_checksum(data))
    connection.close()


def add_checksum(s):
    result = []
    acc = 0
    for hexcode in s.split():
        code = int(hexcode, 16)
        acc += code
        result.append(chr(code))
    result.append(chr(acc))
    return ''.join(result)

master = Tk()
host = StringVar()
Radiobutton(master, text="Silas",indicatoron = 0, variable = host, value ='172.25.13.10').pack(anchor=W)
Radiobutton(master, text="Beatrice", indicatoron = 0, variable = host, value ='172.25.13.12').pack(anchor=W)

#entered_host = StringVar()
#e = Entry(master, textvariable=entered_host)
#e.pack()

b = Button(
    master,
    text="Power On",
    command=lambda: callback_power_on(on, host))
    #command=lambda: callback_power_on(on, host)

b.pack()

c = Button(
    master,
    text="Power Off",
    command=lambda: callback_power_off(off, host))
    #command=lambda: callback_power_on(on, host)
c.pack()

mainloop()

Recommended Answers

All 3 Replies

Perhaps replace host with host.get() in lambda .

You might have set host initially to something that makes sense.

Using partial is easier and makes more sense IMHO Partial examples

from functools import partial

...

## to send "on" and "host" to callback_power_on function
b = Button(master,
           text="Power On",
           command=partial(callback_power_on, on, host))  

##---------- or if you prefer  ----------------------------
b = Button(master,
           text="Power On",
           command=partial(callback_power_on, data=on, host=host))  
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.