2016-08-25 10 views
0

以下のコードに示すように、[同期/待機メカニズムのための]スレッド化メカニズムに基づいてforループを進めたいとします。Forループ内でのスレッディングとTkinter〜Pythonでのイベントの使用

#!/tools/bin/python 

# Global Import Variables 
import Tkinter 
from Tkinter import * 
import subprocess 
import shlex 
import os 
from PIL import Image, ImageTk 
import time 
import string 
import threading 

class test_template: 
    def __init__(self, master): 
     self.master = master 
     self.next_button = None 
     self.fruit_lbl = None 
     self.yes_button = None 
     self.no_button = None 
     self.yes_no_button_done = threading.Event() 

     # For Loop Goes From 0 To 10 
     for i in range (10): 
      if not (self.fruit_lbl): 
       self.fruit_lbl = Label(root, text="Do You Want To Change The Color Of Fruit %d"%i) 
       self.fruit_lbl.grid(row=1, column=0) 
      if not (self.yes_button): 
       self.yes_button = Button(root, background="lawn green", activebackground="forest green", text="YES", command=self.yes_button_code).grid(row=2, column=1)  
      if not (self.no_button): 
       self.no_button = Button(root, background="orange red", activebackground="orangered3", text="NO", command=self.no_button_code).grid(row=2, column=2)  

      while not self.yes_no_button_done.isSet(): 
       self.yes_no_button_done.wait() 

    def yes_button_code(self): 
     print "Inside YES Button Config" 
    def no_button_code(self): 
     print "Inside No Button Config" 
     self.yes_no_button_done.set() 

# Top Local Variables 
root = Tk() 
#root.configure(background='black') 

# Top Level Default Codes 
my_gui = test_template(root) 

root.mainloop() 

そのループのために、私はそれまでwhileループが設定したイベントを待っているべきである、ないか、またはいずれかのはいを押すまで待機する必要がありますように。

しかし、どういうわけか、スレッド/待機メカニズムはうまく動作しません。つまり、すぐにコードを起動すると、whileループに入り、ひそかに行きます。私は上記のコードで何かを見逃していますか?あなたのコメントを共有してください!

+0

スレッドが起動またはそのような何かする必要がありますか?いくつかの例では、self.yes_no_button_done.start()のように表示されるためです。これは必須ですか? whileループでループ待機する理由は何ですか? – Vimo

+0

ハフ...うーん..無残!!!コメントはありますか?推測 ?または、プロセス間通信やスレッド化の方法がありますか? – Vimo

+0

こんにちは、これは一回だけループしますか? 'そうでなければ'と尋ねてきて、最初のループの変数を設定します。 2番目のループは残っているはいまたはいいえボタンでしょうか?私はあなたが決して 'yes_no_button_done'を何かに設定していないことを見ているからです。set()を言っているだけです。私はこのコードで何かをスレッド化する必要がある理由を完全に理解していますか?あなたが達成しようとしていることを簡単に説明できますか? –

答えて

0

私が正しくあなたに納得していれば、あなたはスレッドをまったく使用する必要はないと私は思っています。次の点を考慮してください。

from Tkinter import * 


class test_template: 

    def __init__(self, master): 
     self.master = master 
     self.loop_count = IntVar() 
     self.loop_count.set(1) 

     l = Label(self.master, text='Change colour of fruit?') 
     l.pack() 
     count = Label(self.master, textvariable = '%s' % self.loop_count) 
     count.pack() 
     yes = Button(self.master, text = 'YES', command = self.loop_counter) 
     yes.pack() 
     no = Button(self.master, text = 'NO', command = self.loop_counter) 
     no.pack() 

    def loop_counter(self): 
     if self.loop_count.get() == 10: 
      print 'Finished' 
      self.master.quit() 
     else: 
      self.loop_count.set(self.loop_count.get()+1) 
      print self.loop_count.get() 


root = Tk() 
GUI = test_template(root) 
root.mainloop() 

あなたの意見を教えてください。

乾杯、 ルーク

関連する問題