首页 文章

单选按钮tkinter传递主机值

提问于
浏览
1

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

这是错误:文件"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",第1410行,在 call 中返回self.func(* args)文件"Untitled 2.py",第63行,在命令= lambda:callback_power_off(关闭,主机))文件"Untitled 2.py",第28行,在callback_power_off connection.connect中((主机,端口))文件"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",第224行,在meth返回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 回答

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

相关问题