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ループに入り、ひそかに行きます。私は上記のコードで何かを見逃していますか?あなたのコメントを共有してください!
スレッドが起動またはそのような何かする必要がありますか?いくつかの例では、self.yes_no_button_done.start()のように表示されるためです。これは必須ですか? whileループでループ待機する理由は何ですか? – Vimo
ハフ...うーん..無残!!!コメントはありますか?推測 ?または、プロセス間通信やスレッド化の方法がありますか? – Vimo
こんにちは、これは一回だけループしますか? 'そうでなければ'と尋ねてきて、最初のループの変数を設定します。 2番目のループは残っているはいまたはいいえボタンでしょうか?私はあなたが決して 'yes_no_button_done'を何かに設定していないことを見ているからです。set()を言っているだけです。私はこのコードで何かをスレッド化する必要がある理由を完全に理解していますか?あなたが達成しようとしていることを簡単に説明できますか? –