2016-05-16 6 views
0

のために殺すために関数をクリックして、私は私のGUIアプリケーションを設計するためのThis web sites tutorialsに取り組んでいますが、私は問題に直面し、how do i could arrest the progress bar updating in clicking the Button StopパイソンのGtk:どのように私は、Buttonウィジェットを与えることができるプログレスバーの更新

に実際に私は約Gtk.ProgressBar.set_pulse_step()けどを見ます私は専門家ではないので、私はまだ奇妙に見えます。 ここで私のコードはどこに停止機能がありませんでした。

import gi 
gi.require_version('Gtk', '3.0') 
from gi.repository import Gtk, GObject 

class ProgressBarWindow(Gtk.Window): 

    def __init__(self): 
     Gtk.Window.__init__(self, title="ProgressBar Demo") 
     self.set_border_width(10) 

     vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) 
     self.add(vbox) 

     self.progressbar = Gtk.ProgressBar() 
     vbox.pack_start(self.progressbar, True, True, 0) 

     button = Gtk.Button(label="Start") 
     button.connect("clicked", self.On_clicking) 
     vbox.pack_start(button, True, True, 0) 

     button = Gtk.Button(label="Stop") 
     button.connect("clicked", self.On_clicking_stop) 
     vbox.pack_start(button, True, True, 0) 


    def On_clicking(self, widget): 
     self.timeout_id = GObject.timeout_add(50, self.on_timeout, None) 
     self.activity_mode = False 

    def On_clicking_stop(self, widget): 
     ## I have to stop the Progress Bar on Stop Button click 
     ## 
     ## 
     ## 
     ## 
     ## 
     return False 


    def on_timeout(self, user_data): 
     """ 
     Update value on the progress bar 
     """ 
     if self.activity_mode: 
      self.progressbar.pulse() 
     else: 
      new_value = self.progressbar.get_fraction() + 0.01 

      if new_value > 1: 
       new_value = 0 

      self.progressbar.set_fraction(new_value) 

     # As this is a timeout function, return True so that it 
     # continues to get called 
     return True 

win = ProgressBarWindow() 
win.connect("delete-event", Gtk.main_quit) 
win.show_all() 
Gtk.main() 

私はOn_clicking_stop()の正しいコードを探しています。

答えて

0

GObject.timeout_add(50, self.on_timeout, None)を使用してprogressbarが更新されます。これは、Falseが返されるまで、指定された関数を呼び出し続けるタイムアウト関数です。したがって、プログレスバーの更新を停止させるには、on_timeoutFalseを返すように変更する必要があります。

これは、例えば、次のように行われる:

def On_clicking(self, widget): 
    self.activity_mode = False 
    self.updating = True 
    self.timeout_id = GObject.timeout_add(50, self.on_timeout, None) 


def On_clicking_stop(self, widget): 
    self.updating = False 
    return True 

def on_timeout(self, user_data): 
    """ 
    Update value on the progress bar 
    """ 
    if self.activity_mode: 
     self.progressbar.pulse() 
    else: 
     new_value = self.progressbar.get_fraction() + 0.01 

     if new_value > 1: 
      new_value = 0 

     self.progressbar.set_fraction(new_value) 

    # As this is a timeout function, return True so that it 
    # continues to get called 
    return self.updating 
関連する問題