gpt4 book ai didi

python - 单选按钮 tkinter 传递主机值

转载 作者:行者123 更新时间:2023-12-01 05:30:04 25 4
gpt4 key购买 nike

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

这是错误:文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py”,第 1410 行,调用 返回 self.func(*args) 文件“Untitled 2.py”,第 63 行,位于 命令=lambda:callback_power_off(关闭,主机)) 文件“Untitled 2.py”,第 28 行,callback_power_off 连接.connect((主机,端口)) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py”,第 224 行,在方法中 返回 getattr(self._sock,name)(*args)类型错误:强制转换为 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()

最佳答案

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

关于python - 单选按钮 tkinter 传递主机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20443148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com