Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Entry.get()给出错误,每次都不同。无效的命令名。55_Python_Sockets_Tkinter_Udp_Chat - Fatal编程技术网

Python Entry.get()给出错误,每次都不同。无效的命令名。55

Python Entry.get()给出错误,每次都不同。无效的命令名。55,python,sockets,tkinter,udp,chat,Python,Sockets,Tkinter,Udp,Chat,我正在尝试为学校制作一个简单的聊天窗口。当我尝试使用entry.get()函数时,它会给出一个错误,每次测试代码时都会发生更改。 以下是脚本: import socket import sys import tkinter global ready ready = False def setready(): global ready ready = True window = tkinter.Tk() window.title("MAGENTA Chat Host v1 - s

我正在尝试为学校制作一个简单的聊天窗口。当我尝试使用entry.get()函数时,它会给出一个错误,每次测试代码时都会发生更改。 以下是脚本:

import socket
import sys
import tkinter

global ready
ready = False
def setready():
    global ready
    ready = True
window = tkinter.Tk()
window.title("MAGENTA Chat Host v1 - send")
window.geometry("200x100")
window.configure(bg = "magenta")
but = tkinter.Button(window, text = "Send", command = (setready()))
lbl = tkinter.Label(window, bg = "magenta", fg = "snow", text = "Chat")
msgfield = tkinter.Entry(window)
but.pack(side = tkinter.RIGHT)
lbl.pack(side = tkinter.TOP)
msgfield.pack(side = tkinter.LEFT)
message = "Message"
UDP_IP = "127.0.0.1"

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    lbl.config(text = "Socket successfully created")
except socket.error as err:
    lbl.config(text = "Socket creation failed with error %s" %(err))

window.mainloop()

while True:
    if ready == True:
        message = msgfield.get()
        if message == "exit":
            s.close()
            window.destroy()
            sys.exit()
        else:
            s.sendto(bytes(message, "utf-8"),(UDP_IP,5000))
            msgfield.select_clear()
            ready = False
以下是错误:

Traceback (most recent call last):
  File "C:\Users\qscguest\Desktop\ChatSend.py", line 33, in <module>
    message = msgfield.get()
  File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 2522, in get
    return self.tk.call(self._w, 'get')
_tkinter.TclError: invalid command name ".55573472"
回溯(最近一次呼叫最后一次):
文件“C:\Users\qscguest\Desktop\ChatSend.py”,第33行,在
message=msgfield.get()
get中第2522行的文件“C:\Program Files\Python35\lib\tkinter\\uuuuu init\uuuuuu.py”
返回self.tk.call(self.\w'get')
_tkinter.TclError:无效的命令名“.55573472”

我怎么搞得这么糟?如何修复此问题?

window.mainloop()
在根窗口被销毁之前不会退出。此后任何试图引用小部件的代码都将失败,因为小部件已被销毁

我想你是想让你的tkinter应用程序在你键入exit并点击send时关闭程序,如果还有其他功能,也可以执行其他功能。我已经修改了您的代码,以执行我认为您正在尝试的操作

我改变了一些事情

我去掉了全局函数,因为它不需要,因为我们可以在循环时将
ready
移动到定义的函数

我更改了按钮以使用lambda运行命令
myWhile()
loop,这样在启动时就不会调用myWhile

我添加了一个名为
myWhile()
的函数来容纳while循环并执行您需要完成的任务。让我知道这是否是你一直在寻找的,因为我不是100%确定你的需求

import socket
import sys
import tkinter

window = tkinter.Tk()
window.title("MAGENTA Chat Host v1 - send")
window.geometry("200x100")
window.configure(bg = "magenta")
but = tkinter.Button(window, text = "Send", command = lambda: myWhile())
lbl = tkinter.Label(window, bg = "magenta", fg = "snow", text = "Chat")
msgfield = tkinter.Entry(window)
but.pack(side = tkinter.RIGHT)
lbl.pack(side = tkinter.TOP)
msgfield.pack(side = tkinter.LEFT)
message = "Message"
UDP_IP = "127.0.0.1"

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    lbl.config(text = "Socket successfully created")
except socket.error as err:
    lbl.config(text = "Socket creation failed with error %s" %(err))


def myWhile():
    ready = True
    while ready == True:
        message = msgfield.get()
        print ('ready')
        if message == "exit":
            s.close()
            window.destroy()
            sys.exit()
        else:
            s.sendto(bytes(message, "utf-8"),(UDP_IP,5000))
            msgfield.select_clear()
            ready = False

window.mainloop()

由于在
setready
内部执行的是
command=setready()
,因此存在无限递归。您的函数正在调用自身。那不是你想要的。您希望将
command
设置为函数本身,而不是函数的返回值:
command=setready
。没有任何更改。同样的错误。代码的缩进不正确,使得不可能知道什么代码在
setready
中,什么不是。@BryanOakley True,我没有考虑函数在
ready=True
之后立即结束的可能性。尽管如此,
command=setready()
仍然是不正确的,不管实际缩进是什么。修复了这个问题。我明白你的意思了,但是我应该把它放在哪里?如果我把它放在最后,那也不会起作用。