【问题标题】:radio buttons tkinter passing host value单选按钮 tkinter 传递主机值
【发布时间】:2013-12-24 22:34:23
【问题描述】:

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

这是错误: 调用中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py”,第 1410 行 返回 self.func(*args) 文件“Untitled 2.py”,第 63 行,在 command=lambda: callback_power_off(off, host)) 文件“Untitled 2.py”,第 28 行,在 callback_power_off 中 连接.连接((主机,端口)) 文件“/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()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    我认为问题在于 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()
    

    【讨论】:

    • 这很有意义! .get() 方法是否总是需要成为我的回调参数的一部分?
    • 并非如此,您只需要确保使用像callback_power_on(on, '127.0.0.1') 这样的字符串。或者,您可以更改您的函数以接受 StringVar 实例并在函数内部进行转换。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 2015-10-04
    • 2014-05-08
    • 2021-04-14
    • 2023-03-27
    • 2014-12-18
    • 1970-01-01
    相关资源
    最近更新 更多