Tkinter窗口没有关闭

时间:2020-07-29 21:51:17

标签: python tkinter

我正在用Tkinter制作服务器,它在其中询问您要启动服务器的IP地址。当您输入jit时,应该关闭窗口并启动服务器,但是窗口没有关闭,只会崩溃(尽管服务器也会启动)。 我的代码:

#!/usr/bin/env python3
import socket, threading, sys
import tkinter as tk

def sub(self):
    global ip
    ip = e1.get()
    rw.quit()


rw = tk.Tk()
rw.title("IP")
rw.configure(bg="grey14")
rw.geometry("250x50")

hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)

e1 = tk.Entry(rw)
e1.bind("<Return>", sub)
e1.insert(0, IPAddr)
e1.pack(side=tk.LEFT)

b1 = tk.Button(rw, text="Submit", command=sub).pack(side=tk.RIGHT)

rw.mainloop()

请帮助! (服务器代码被省略了,因为StackOverflow表示不允许我拥有那么多的代码和这么小的文本,但是基本上,它启动了另一个接受客户端的循环)

2 个答案:

答案 0 :(得分:1)

您应该改用以下命令:

rw.destroy()

rw.quit()只是绕过了rw.mainloop(),因此rw.mainloop()仍将在后台运行。 rw.destroy()将其停止。

答案 1 :(得分:-1)

您正在使用sub(self),而在您的Button上您只写了command=sub 所以你必须写他们的


self.b1=tk.Button(rw,text="Submit",command=sub).pack(side=tk.RIGHT)

我已经运行它了