单选按钮tkinter传递主机值

时间:2013-12-07 15:37:27

标签: python tkinter

尝试使用单选按钮选择我所指的主机,因为最终会有 只有两个,它们是固定的地址。

这是错误: 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py”,第1410行,致电     return self.func(* args)   文件“Untitled 2.py”,第63行,in     command = lambda:callback_power_off(off,host))   在callback_power_off中输入第28行的“Untitled 2.py”     connection.connect((host,port))   文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py”,第224行,在meth     return getattr(self._sock,name)(* args) TypeError:强制转换为Unicode:需要字符串或缓冲区,找到实例

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", variable = host, value ="172.25.13.10").pack(anchor=W)
Radiobutton(master, text="Beatrice", 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()

1 个答案:

答案 0 :(得分:2)

我认为问题是connect需要一个字符串,但是你传递的是StringVar个实例。使用StringVar方法get()检索字符串值。

以下是您的示例的略微变化,请注意在按钮命令功能中使用entered_host.get()

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

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

b = Button(
    master,
    text="Power On",
    command=lambda: callback_power_on(on, entered_host.get()))
b.pack()

c = Button(
    master,
    text="Power Off",
    command=lambda: callback_power_off(off, entered_host.get()))
c.pack()

mainloop()