Python 如何使用顶级方法?

Python 如何使用顶级方法?,python,tkinter,tk,toplevel,Python,Tkinter,Tk,Toplevel,嗨,我试着在tkinter中使用顶级方法,但它不起作用。。。 当有方法并行运行时,我应该写什么来在不同的时间用两种方法打开两个窗口? 确定与接收方法并行运行的方法。。。代码卡在window=Toplevelroot的接收方法中。当然,这是一个消息,但我不想让你们这些家伙 from Tkinter import * import threading def decide_what(self): global root root = Tk() root.title("

嗨,我试着在tkinter中使用顶级方法,但它不起作用。。。 当有方法并行运行时,我应该写什么来在不同的时间用两种方法打开两个窗口? 确定与接收方法并行运行的方法。。。代码卡在window=Toplevelroot的接收方法中。当然,这是一个消息,但我不想让你们这些家伙

from Tkinter import *
import threading


def decide_what(self):

    global root
    root = Tk()

    root.title("options")
    root.geometry("600x250")
    root.resizable(width=FALSE, height=FALSE)  # cant resize

    self.label = Label(root, text='CHOOSE YOUR FIRST OPTION!', font=30)
    self.label.place(x=200, y=13)

    self.button1 = Button(root, text='PrivateChat', font=30, 
    command=self.private)
    self.button1.place(x=1, y=50, width=200, height=199)

    self.button2 = Button(root, text='GroupChat', font=30, 
    command=self.group)
    self.button2.place(x=201, y=50, width=199, height=199)

    self.button3 = Button(root, text='BroadCast', font=30, 
    command=self.broadcast)
    self.button3.place(x=400, y=50, width=200, height=199)

    self.button4 = Button(root, text='WAIT', font=30, command=self.wait)
    self.button4.place(x=500, y=10)

    root.mainloop()


def receiving_message(self):  # a function that responsible to receive a message from the server, **shes in a class**

    print "receive??????????????????"
    while True:

        data = self.sock.recv(1024)

        data = decryption(data)
        print "data", data

        if data[:2] == "Br":

            print "got into br"

            window = Toplevel(root)
            print "window V"

            window.title("BroadCastZone")
            label = Label(window, text=data)
            label.pack()
            button = Button(window, text="ok", command=window.destroy)
            button.pack()

            print data

所有tkinter代码都需要在同一线程中运行。如果接收_消息在单独的线程中运行,则无法创建Toplevel的实例。它需要向主线程发送一条消息,并要求它打开一个窗口

你确定你不仅仅是被困在无限循环中吗?套接字侦听器可能应该在GUI的一个单独的线程或进程中运行。“while True”语句是一个阻塞语句,这意味着它将阻止事件循环root.mainloop处理更新显示、创建窗口等的事件。因此,您需要一个单独的侦听器线程与GUI通信,让GUI可以格式化和显示结果。但它位于一个单独的线程中。。。GUI和套接字不会发生冲突,所以如果我必须这样做,那么线程中的新窗口更好吗?