2017-03-20 7 views
0

私は、ユーザーがいくつかのファイルを選択して、オプションなどをマークするメインのtkinterウィンドウを持っています。 Runボタンを押すには、3つの事が起こる必要があります。Tkinterメインループブロッキング効果

  • をメインウィンドウが
  • 背景proccesを消え、新しいウィンドウが表示され、(で実行中のプロセスに関する情報を印刷するために開始し
  • 中に実行を開始例:bは3、bは5、bは10など

2つのスレッドを使用せずにこれを行う方法はありますか(もしそうなら、どのように試みましたか?どのようにプロセスを終了するか)GUIとプロセスから1つ。私が知る限り、tkinter mainloopブロッキング効果を有する。 アドバイスをいただきありがとうございます。 次のコードは明らかになります。各ウィンドウにクラスがあります。

import tkinter as Tk 
import time 

class Mainframe(object): 


    def __init__(self,parent): 
     self.root=parent 
     self.root.title("Main frame") 
     self.frame=Tk.Frame(parent) 
     self.frame.pack() 

     btn=Tk.Button(self.frame,text="Run momma",command=self.RunButton) 
     btn.pack() 

    def Secondwindow(self): 

     self.root.destroy() 
     newWindow=Tk.Tk() 
     window=Secondframe(newWindow) 


    def process(self): 

     b=0 
     for a in range(0,20): 
      b=a 
      print (b) 
      time.sleep(0.2) 

    def RunButton(self): 

     self.Secondwindow() 
     self.process() 


class Secondframe(object): 

    def __init__(self,parent): 
     self.root=parent 
     self.root.title("Main frame") 
     self.frame=Tk.Frame(parent) 
     self.frame.pack() 
     self.root.geometry("400x300") 
     self.root.title("otherFrame") 

if __name__=="__main__": 

    root=Tk.Tk() 
    root.geometry("800x600") 
    app=Mainframe(root) 
    root.mainloop() 

答えて

0

はい、あなたは一つのウィンドウを殺すと(withdrawToplevelを使用して、何度もTk()以上を呼び出すことはありません)別のブートが、なぜあなたはそれを行うだろうか?なぜ最初のウィンドウを更新しないのですか?

import tkinter as tk 
import time 
import threading 

class Mainframe(tk.Tk): 
    def __init__(self): 
     tk.Tk.__init__(self) 
     self.frame = FirstFrame(self) 
     self.frame.pack() 

    def change(self, frame): 
     self.frame.pack_forget() # delete currrent frame 
     self.frame = frame(self) 
     self.frame.pack() # make new frame 

class FirstFrame(tk.Frame): 
    def __init__(self, master=None, **kwargs): 
     tk.Frame.__init__(self, master, **kwargs) 

     master.title("Main frame") 
     master.geometry("800x600") 

     btn=tk.Button(self,text="Run momma",command=self.RunButton) 
     btn.pack() 

    def RunButton(self): 
     self.master.change(SecondFrame) 

class SecondFrame(tk.Frame): 
    def __init__(self, master=None, **kwargs): 
     tk.Frame.__init__(self, master, **kwargs) 
     master.title("otherFrame") 
     master.geometry("400x300") 
     self.var = tk.StringVar(self) 
     lbl = tk.Label(self, textvariable=self.var) 
     lbl.pack(anchor='w') 
     btn = tk.Button(self, text="go back", command=lambda: master.change(FirstFrame)) 
     btn.pack() 

     t = threading.Thread(target=self.process) 
     t.start() 


    def process(self): 
     b=0 
     for a in range(0,20): 
      b=a 
      print (b) 
      self.var.set("Current value is {}".format(b)) 
      time.sleep(0.2) 

if __name__=="__main__": 
    app=Mainframe() 
    app.mainloop() 

注意まだスレッドを使用しています。これは、GUIがロックアップするため、tkinterを使用しているときにブロッキングループに入ることができないためです。すべての長い計算をtkinterメインループ(通常はafterメソッドを使用)に組み込むか、それ自身のスレッドにする必要があります。

+0

ブリリアント!ありがとう。私は非常にプログラミングに新しいし、すべての内部を知っていない、例えば、なぜあなたは一度も何度もtk()を呼び出すべきではない。私はまた、後のメソッドを使用するように見えましたが、私は一度だけ実行させる方法を理解できませんでした。とにかく、答えのためのthxは、これを理解することに私を多く助けるだろう:D – Mike