2017-06-06 81 views
0

嗨我試圖在tkinter中使用onplevel方法,它沒有工作... 我應該寫什麼來打開兩個窗口在不同的時間在兩種方法,而有方法並行運行? 決定與接收方法並行運行的方法...代碼卡在「window = Toplevel(root)」的接收方法中。當然它得到一個消息,但我不想溢出你們...我如何使用Toplevel方法?

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 
+1

你確定你不會陷入無限循環嗎? – acdr

+2

套接字偵聽器應該可能在GUI中的單獨線程或進程中運行。 'while True'語句是一個阻塞語句,這意味着它將阻止事件循環(root.mainloop)處理更新顯示,創建窗口等事件。因此,您需要一個單獨的偵聽器線程與GUI進行通信,從而離開GUI可用於格式化和顯示結果。 –

+0

但它在一個單獨的線程... GUI和套接字不碰撞 – shahar

回答

1

所有tkinter代碼需要運行在同一個線程。如果receiving_message正在單獨的線程中運行,則無法創建Toplevel的實例。它需要發送消息給主線程並要求它打開一個窗口。

+0

因此,如果我必須這樣做,線程中的新窗口更好? – shahar