2016-04-23 7 views
1

Python 2.7でループが実行されているときにボタンをクリックすると、Pythonがループから出てくるまでIDLEが動作を停止します。なぜ私はこれが起こっているのか分からないので、私はコード全体を添付しました。 Pythonはループの外に来るまで、私は停止やボタンの作品をクリックすると現在のループがPythonで終了するまでボタンが動作しない

import time 
import Tkinter as tk 
from Tkinter import StringVar 
import threading 

x="False" 

def xval(*args): 
    for i in range(0,9): 
     global x 
     if(x=="False"): 
      print "x=false %d time"%i 
      time.sleep(1) 

def stop(event): 
       resume_btn.configure(state="normal") 
       global x 
       x ="True" 
       print "execution stopped:%s"%x 

def start(event): 
       global x 
       x ="False" 
       print "execution started:%s"%x 
       xval() 

root = tk.Tk() 

th = threading.Event() 
t = threading.Thread(target=xval,args=(th,)) 
t.deamon=True 
t.start() 

x_btn = tk.Button(root, text="Stop", background="Snow", width=20) 
x_btn.grid(row=0, column=4, sticky="W", padx=20, pady=5) 
x_btn.bind('<Button-1>',stop) 

resume_btn = tk.Button(root, text="Start", background="Snow", width=20) 
resume_btn.configure(state="disabled") 
resume_btn.grid(row=0, column=6, sticky="W", padx=20, pady=5) 
resume_btn.bind('<Button-1>',start) 

root.mainloop() 

は、ここで、両方のボタンが更新されます最初に行くが、第2の時間もないxの値で正常に動作します。誰がなぜこれが起こっているのかを伝えることはできますか?

答えて

1

はい、プログラムはfor()を実行してから何かを実行します。これを回避するには、スレッドとメインプログラムの両方でリアルタイムで共有できるいくつかのコンテナを使用する必要があります(マルチプロセッシングではマネージャの辞書またはリストです。 Tkinterのafterメソッドを使って、クラスのインスタンスオブジェクト/属性(このコードでは変数)を使用する、以下のコードに似たようなことをすることができます。 http://www.tutorialspoint.com/python/python_classes_objects.htmのみ(variable.getを使用するために必要な

import Tkinter as tk 

class StartStop(): 
    def __init__(self, root): 
     self.x="False" 
     self.ctr=0 

     x_btn = tk.Button(root, text="Stop", background="Snow", width=20) 
     x_btn.grid(row=0, column=4, sticky="W", padx=20, pady=5) 
     x_btn.bind('<Button-1>', self.stop) 

     self.resume_btn = tk.Button(root, text="Start", background="Snow", width=20) 
     self.resume_btn.configure(state="disabled") 
     self.resume_btn.grid(row=0, column=6, sticky="W", padx=20, pady=5) 
     self.resume_btn.bind('<Button-1>', self.start) 


    def xval(self): 
     if self.x=="False": 
      print "x=false %d=counter value"%self.ctr 
      self.ctr += 1 
      if self.ctr < 9: 
       ## after gives the program time to update 
       ## time.sleep() stops everyting 
       root.after(1000, self.xval) 

    def stop(self, event): 
      self.resume_btn.configure(state="normal") 
      self.x ="True" 
      print "execution stopped:%s"%self.x 

    def start(self, event): 
      self.x ="False" 
      print "execution started:%s"%self.x 
      self.ctr=0 
      self.xval() 

root = tk.Tk() 
S=StartStop(root) 
root.mainloop() 
+0

を行うには良い方法であると言うでしょう:/それは、クラスを使用せずに可能ですか? – YSR

+0

クラスは良い方法です。 @ YSR –

+0

:私は知っているし、私は確かに@BillalBEGUERADJだろうが、私はこのコードが必要な私のプロジェクトでそれらを使用していない^ _^'それがクラスなしでできるかどうか尋ねていた理由 – YSR

1

)及び設定()root.updateと共に()ループの終わりに。

import time 
import Tkinter as tk 
from Tkinter import StringVar 
import threading 
global root 
root = tk.Tk() 
x = tk.StringVar() 
x.set('false') 

def xval(*args): 
    try: 
     for i in range(0,9): 
      global x 
      print x.get() 
      if x.get()== 'false' : 
       print "x=false %d time"%i 
       time.sleep(1) 
      else: 
       print "waiting" 
      root.update() 
    except: 
     pass 

def stop(event): 
       resume_btn.configure(state="normal") 
       global x 
       x.set('true') 
       print "execution stopped:%s"%x 


def start(event): 
       global x 
       x.set('false') 
       print "execution started:%s"%x 
       xval() 


root.title("GUI-Data Retrieval") 
th = threading.Event() 
t = threading.Thread(target=xval,args=(th,)) 
t.deamon=True 
t.start() 
x_btn = tk.Button(root, text="Stop", background="Snow", width=20) 
x_btn.grid(row=0, column=4, sticky="W", padx=20, pady=5) 
x_btn.bind('<Button-1>',stop) 
resume_btn = tk.Button(root, text="Start", background="Snow", width=20) 
resume_btn.configure(state="disabled") 
resume_btn.grid(row=0, column=6, sticky="W", padx=20, pady=5) 
resume_btn.bind('<Button-1>',start) 
root.mainloop() 

しかし、私はクラスがこの:)

関連する問題