2012-12-01 32 views
5

चल रहा है मैं पाइथन में स्क्रिप्ट बना रहा हूं, जो बाश के साथ सहयोग करता है। जब मैं सभी खोज विकल्प सेट करता हूं और खोज बटन दबाता हूं, तो मैं progress bar के साथ पॉपअप विंडो दिखाना चाहता हूं, जो खोज पूरा होने पर गायब हो जाएगा। मैं popup.show() द्वारा खोलता हूं और जब तक मैं पॉपअप बंद नहीं करता तब तक कोई फ़ंक्शन निष्पादित नहीं होता है। तो इस समस्या को हल करने के लिए कैसे?pygtk दो खिड़कियां, पॉपअप और मुख्य

नियंत्रक वर्ग में:

def search(self, widget): 
    cmd = "find " + self.model.directory + " -name \"" + self.model.name + "\"" + " -perm -" + str(self.model.mode) 
    if self.model.type is not None and self.model.type != '': 
     cmd += " -type " + self.model.type 
    if self.model.owner is not None: 
     cmd += " -user " + self.model.owner 
    if self.model.days is not None: 
     cmd += " -mtime -" + str(self.model.days) 

    self.progress = SearcherProgressBar() 

    output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) 
    out = output.stdout.read().strip() 
    array = string.split(out, '\n') 
    self.list = list() 
    self.progress.label = "sdsds" 
    for value in array: 
     self.add_to_list(value) 

    #self.progress.popup.destroy() # when I uncomment, popup never appears 

    self.view.treestore.clear() 
    self.add_to_tree(self.list, None) 
    self.view.expand_item.set_sensitive(True) 

प्रगति बार कक्षा में:

class SearcherProgressBar: 

def __init__(self): 
    self.popup = gtk.Window(gtk.WINDOW_POPUP) 
    vbox = gtk.VBox() 
    self.popup.add(vbox) 
    self.popup.set_size_request(500,100) 
    self.label = gtk.Label("Searching...") 
    vbox.pack_start(self.label, True, True, 0) 
    self.popup.connect("destroy", self.dest) 
    self.popup.show_all() 


def dest(self, widget, data=None): 
    self.popup.destroy() 
    return False 
+3

अपने कोड बनाने [SSCCE - लघु, स्व निहित, सही (compilable), उदाहरण] (http://sscce.org/)। हम आपके प्रश्न में आपके पास कोड नहीं चला सकते हैं। वे सिर्फ आपके आवेदन के हिस्से के टुकड़े हैं। –

उत्तर

1

[हल] इस कोड को प्रत्येक विजेट का अद्यतन

while gtk.events_pending(): 
     gtk.main_iteration() 
1

किसी भी दर के बाद जोड़ा जाना चाहिए, सवाल काफी रोचक लग रहा था हालांकि यह देखने के लिए बाहर से मुश्किल है कि आप क्या चाहते हैं या आपका प्रोग्राम कैसे काम करता है और काम करने का इरादा रखता है। मैं एक पॉपअप कैसे बना सकता हूं इसके लिए मैंने एक न्यूनतम कोड रखा है।

मैं गुई के लिए bash-command और threading के लिए subprocessing के संयोजन का उपयोग करता हूं।

महत्वपूर्ण बिट्स इस कोड में गौर करने योग्य

  • threading के बाद से gtk के साथ संयोजन में प्रयोग किया जाता है हम gobject.threads_init()

  • कार्यों है कि जल्दी से निष्पादित धागे में नहीं हैं और रिटर्न को नियंत्रित करने का उपयोग करना चाहिए gtk.main -loop

  • सर्च-बटन निष्क्रिय हो गया है जबकि subprocesses को ढेर नहीं किया गया है।

और यहाँ कोड है:

#!/usr/bin/env python 

import gtk 
import threading 
import gobject 
from subprocess import Popen, PIPE 

class SearcherProgressBar(object): 
    """This is the popup with only progress-bar that pulses""" 
    def __init__(self): 
     self._popup = gtk.Window(gtk.WINDOW_POPUP) 
     self._progress = gtk.ProgressBar() 
     self._progress.set_text = gtk.Label("Searching...") 
     self._popup.add(self._progress) 

    def run(self, search, target): 
     """Run spawns a thread so that it can return the process to the 
     main app and so that we can do a little more than just execute 
     a subprocess""" 
     self._popup.show_all() 
     self._thread = threading.Thread(target=self._search, args=(search, target)) 
     self._thread.start() 

     #Adding a callback here makes gtk check every 0.42s if thread is done 
     gobject.timeout_add(420, self._callback) 

    def _search(self, cmd, target): 
     """This is the thread, it makes a subprocess that it communicates with 
     when it is done it calls the target with stdout and stderr as arguments""" 
     p = Popen(cmd, stdout=PIPE, stderr=PIPE) 
     target(*p.communicate()) 

    def _callback(self, *args): 
     """The callback checks if thread is still alive, if so, it pulses 
     return true makes callback continue, while false makes it stop""" 
     if self._thread.is_alive(): 
      self._progress.pulse() 
      return True 
     else: 
      self._popup.destroy() 
      return False 

class App(object): 

    def __init__(self): 

     self._window = gtk.Window() 
     self._window.connect("destroy", self._destroy) 

     vbox = gtk.VBox() 
     self._window.add(vbox) 

     self.button = gtk.Button() 
     self.button.set_label('Pretend to search...') 
     self.button.connect('clicked', self._search) 
     vbox.pack_start(self.button, False, False, 0) 

     entry = gtk.Entry() 
     entry.set_text("This is here to show this gui doesn't freeze...") 
     entry.connect("changed", self._gui_dont_freeze) 
     vbox.pack_start(entry, False, False, 0) 

     self._results = gtk.Label() 
     vbox.pack_start(self._results, False, False, 0) 

     self._gui_never_freeze = gtk.Label("Write in entry while searching...") 
     vbox.pack_start(self._gui_never_freeze, False, False, 0) 

     self._window.show_all() 

    def run(self): 

     gtk.main() 

    def _gui_dont_freeze(self, widget): 

     self._gui_never_freeze.set_text(
      "You've typed: '{0}'".format(widget.get_text())) 

    def _search(self, widget): 
     """Makes sure you can't stack searches by making button 
     insensitive, gets a new popup and runs it. Note that the run 
     function returns quickly and thus this _search function too.""" 
     self.button.set_sensitive(False) 
     self._results.set_text("") 
     popup = SearcherProgressBar() 
     popup.run(['sleep', '10'], self._catch_results) 

    def _catch_results(self, stdout, stderr): 
     """This is where we do something with the output from the subprocess 
     and allow button to be pressed again.""" 
     self.button.set_sensitive(True) 
     self._results.set_text("out: {0}\terr: {1}".format(stdout, stderr)) 

    def _destroy(self, *args): 

     self._window.destroy()   
     gtk.main_quit() 

if __name__ == "__main__": 

     #This is vital for threading in gtk to work correctly 
     gobject.threads_init() 
     a = App() 
     a.run() 
+0

उत्तर के लिए बहुत बहुत धन्यवाद। मैंने अपनी पोस्ट के ऊपर शॉर्ट कोड के साथ अपना प्रोग्राम समाप्त किया, लेकिन मैं निश्चित रूप से आपके कोड का विश्लेषण करूंगा और मैं कुछ अजगरों में धागे के बारे में कुछ सीखूंगा। – dragon7