2017-06-06 9 views
0

こんにちは、tkinterでtoplevelメソッドを使用しようとしましたが、それは作業をしませんでした... 2つのメソッドで異なる時間に2つのウィンドウを開くには、どうすればよいですか? 受信メソッドと並行して実行されているメソッドを決定する...コードは受信メソッドの "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とソケットdoesntの衝突 – shahar

答えて

1

すべてのtkinterコードは同じスレッドで実行する必要があります。 receiving_messageが別のスレッドで実行されている場合、Toplevelのインスタンスを作成することはできません。メインスレッドにメッセージを送信し、ウィンドウを開くように要求する必要があります。

+0

私はスレッドの新しいウィンドウのようにそれを行う必要がある場合は良いですか? – shahar

関連する問題