Partage
  • Partager sur Facebook
  • Partager sur Twitter

[PyGTK] Problème lors de la fermeture d'un filechooserdialog

    3 août 2011 à 18:41:56

    Bonsoir,

    Actuellement en train de commencer avec PyGTK j'ai un souci lorsque je clic sur mon menu Fichier>Ouvrir, ça ouvre bien la boite de dialogue de sélection d'un fichier, lorsque je clic sur la croix pour fermer la fenêtre ça ferme bien la fenêtre et lorsque je refais Fichier>Ouvrir mon programme se ferme (fenêtre principal du programme) et je ne vois pas du tout pourquoi ça fait ça.

    Edit : Bon au final sur toute les fenêtre que je crée ça fait la même chose :/ J'ai erreur de segmentation dans l'interpréteur

    Voila mon code :

    glade :

    <?xml version="1.0" encoding="UTF-8"?>
    <interface>
      <requires lib="gtk+" version="2.24"/>
      <!-- interface-naming-policy project-wide -->
      <object class="GtkFileChooserDialog" id="filechooserdialog">
        <property name="can_focus">False</property>
        <property name="border_width">5</property>
        <property name="type_hint">dialog</property>
        <child internal-child="vbox">
          <object class="GtkVBox" id="dialog-vbox1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="spacing">2</property>
            <child internal-child="action_area">
              <object class="GtkHButtonBox" id="dialog-action_area1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="layout_style">end</property>
                <child>
                  <placeholder/>
                </child>
                <child>
                  <placeholder/>
                </child>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="pack_type">end</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <placeholder/>
            </child>
          </object>
        </child>
      </object>
      <object class="GtkWindow" id="mainWindow">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="title" translatable="yes">Ma fenetre</property>
        <property name="window_position">center</property>
        <property name="default_width">800</property>
        <property name="default_height">600</property>
        <signal name="destroy" handler="on_mainWindow_destroy" swapped="no"/>
        <child>
          <object class="GtkVBox" id="vbox1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuBar" id="menubar1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <child>
                  <object class="GtkMenuItem" id="menuitem1">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="use_action_appearance">False</property>
                    <property name="label" translatable="yes">_Fichier</property>
                    <property name="use_underline">True</property>
                    <child type="submenu">
                      <object class="GtkMenu" id="file_menu">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <child>
                          <object class="GtkImageMenuItem" id="file_open_menu">
                            <property name="label">gtk-open</property>
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <property name="use_action_appearance">False</property>
                            <property name="use_underline">True</property>
                            <property name="use_stock">True</property>
                            <signal name="activate" handler="on_file_open_menu_activate" swapped="no"/>
                          </object>
                        </child>
                        <child>
                          <object class="GtkSeparatorMenuItem" id="separatormenuitem1">
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <property name="use_action_appearance">False</property>
                          </object>
                        </child>
                        <child>
                          <object class="GtkImageMenuItem" id="file_exit_menu">
                            <property name="label">gtk-quit</property>
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <property name="use_action_appearance">False</property>
                            <property name="use_underline">True</property>
                            <property name="use_stock">True</property>
                            <signal name="activate" handler="on_file_exit_menu_activate" swapped="no"/>
                          </object>
                        </child>
                      </object>
                    </child>
                  </object>
                </child>
                <child>
                  <object class="GtkMenuItem" id="menuitem4">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="use_action_appearance">False</property>
                    <property name="label" translatable="yes">Aid_e</property>
                    <property name="use_underline">True</property>
                    <child type="submenu">
                      <object class="GtkMenu" id="menu3">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <child>
                          <object class="GtkImageMenuItem" id="imagemenuitem10">
                            <property name="label">gtk-about</property>
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <property name="use_action_appearance">False</property>
                            <property name="use_underline">True</property>
                            <property name="use_stock">True</property>
                          </object>
                        </child>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <placeholder/>
            </child>
            <child>
              <placeholder/>
            </child>
            <child>
              <placeholder/>
            </child>
          </object>
        </child>
      </object>
    </interface>
    


    Mon .py :
    #!/usr/bin/env python
    # -*-coding:utf-8 -*
    
    import pygtk
    pygtk.require("2.0")
    import gtk
    
    class MainWindow:    
        def __init__(self):
            gui = gtk.Builder()
            gui.add_from_file("glade/mainWindow.glade")
            
            #self.file_exit_menu = gui.get_object("file_exit_menu")
            
            gui.connect_signals(self)
            
            self.fileDialogChoose = gui.get_object("filechooserdialog")
        
        #Détruit la fenêtre
        def on_mainWindow_destroy(self, widget):
            gtk.main_quit()
            print("quitter")
        
        #Ouverture d'une boite de dialogue
        def on_file_open_menu_activate(self, widget):
            self.fileDialogChoose.show()
        
        #Détruit la fenêtre
        def on_file_exit_menu_activate(self, widget):
            self.on_mainWindow_destroy(widget)
        
    
    if __name__ == '__main__':
        MainWindow()
        gtk.main()
    


    Merci d'avance.
    • Partager sur Facebook
    • Partager sur Twitter
      4 août 2011 à 21:36:24

      Bonsoir,

      En fait c'est tout a fait normal que tu obtienne un segmentation fault puisque lors du clic sur la croix rouge tu détruit la fenêtre en question. Tu peux soit essayer d'écrire directement le code ( c'est pas bien long ;) ) :

      Deja on crée la fenetre "ouvrir" :
      ouvrir = gtk.FileChooserDialog("Ouverture de fichiers", self.fenetre, gtk.FILE_CHOOSER_ACTION_OPEN, ((gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)))
      


      Ensuite il te faut une variable qui va contenire la réponse de l'utilisateur, c'est à dire si il ouvre un fichier ou tout simplement qu'il annule :
      reponse = ouvrir.run()
      


      Et la tu peut traiter les différents choix de la part de l'utilisateur :
      if reponse == gtk.RESPONSE_OK:
          print "OK"
      elif reponse == gtk.RESPONSE_CANCEL:
          print "CANCEL"
      


      Une fois le traitement terminé il faut détruire la fenetre que tu as crée au risque d'en créer une a chaque ouverture de fichier :
      ouvrir.destroy()
      


      Et sinon en passant par pyGTK tu active ta fenêtre avec show() et tu la cache avec hide(). Par contre l'astuce viens du fait qu'il faut intercepter l’événement de destruction : "delete-event" et non le signal "destroy". Le delete-event correspond au moment entre lequel tu a clic la croix et la fenêtre qui va se détruire. Tu intercepte ce signal et au lieu de détruire la fenêtre tu la cache ;)
      • Partager sur Facebook
      • Partager sur Twitter
        5 août 2011 à 1:22:29

        Merci de ta réponse :)

        Bah oui au final cet après midi j'ai réussi en écrivant le code ;)

        Donc pour utiliser le filechooserdialog avec glade il faut utiliser delete-event ?
        Mais cacher la fenêtre avec hide ne consomme pas plus de ressource que de la détruire ?
        Et j'ai essayé en utilisant pygobject qui est conseillé plutôt que pygtk (je ne sais pas pourquoi d'ailleurs :/) mais il y a certaines constantes qui ont changé apparament.

        Voila mon nouveau code sinon (Et par la même occasion si tu pouvais me dire si mon code peut être optimisé et comment ? Bon il n'y a pas grand chose mais si je peux déjà "bien coder"):

        #!/usr/bin/env python
        # -*-coding:utf-8 -*
        
        #from gi.repository import Gtk
        import pygtk
        pygtk.require("2.0")
        import gtk as Gtk
        
        class MainWindow:    
            def __init__(self):
                gui = Gtk.Builder()
                gui.add_from_file("main.glade")
                self.fenetre = gui.get_object("mainWindow")
                self.txtFile = gui.get_object("txt_file")
                gui.connect_signals(self)
                
            def on_menu_file_open_activate(self, widget):
                filter = Gtk.FileFilter()
                filter.set_name("Fichier thème (.mtz)")
                filter.add_pattern("*.mtz")
                fileChooserDialog = Gtk.FileChooserDialog("Ouverture de fichier", self.fenetre, Gtk.FILE_CHOOSER_ACTION_OPEN, ((Gtk.STOCK_CANCEL, Gtk.RESPONSE_CANCEL, Gtk.STOCK_OPEN, Gtk.RESPONSE_OK)))
                fileChooserDialog.add_filter(filter)
                
                reponse = fileChooserDialog.run()
                if reponse == Gtk.RESPONSE_OK:
                    self.file = fileChooserDialog.get_filename()
                    self.txtFile.set_text(self.file)
                fileChooserDialog.destroy()
            
            def on_menu_file_exit_activate(self, widget):
                dialog = Gtk.MessageDialog(self.fenetre, 0, Gtk.MESSAGE_QUESTION, Gtk.BUTTONS_YES_NO, "Etes-vous sur de vouloir quitter l'application ?")
                reponse = dialog.run()
                if reponse == Gtk.RESPONSE_YES:
                    self.on_mainWindow_destroy(widget)
                dialog.destroy()
            
            def on_btn_open_clicked(self, widget):
                self.on_menu_file_open_activate(widget)
            
            def on_btn_exit_clicked(self, widget):
                self.on_mainWindow_destroy(widget)
            
            def on_mainWindow_destroy(self, widget):
                Gtk.main_quit()
            
        
        if __name__ == '__main__':
            MainWindow()
            Gtk.main()
        


        <?xml version="1.0" encoding="UTF-8"?>
        <interface>
          <requires lib="gtk+" version="2.24"/>
          <!-- interface-naming-policy project-wide -->
          <object class="GtkFileFilter" id="filefilter1"/>
          <object class="GtkWindow" id="mainWindow">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="title" translatable="yes">Theme Translate</property>
            <property name="window_position">center</property>
            <property name="default_width">300</property>
            <property name="default_height">100</property>
            <signal name="destroy" handler="on_mainWindow_destroy" swapped="no"/>
            <child>
              <object class="GtkVBox" id="vbox1">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <child>
                  <object class="GtkMenuBar" id="menu">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkMenuItem" id="menu_file">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_action_appearance">False</property>
                        <property name="label" translatable="yes">_Fichier</property>
                        <property name="use_underline">True</property>
                        <child type="submenu">
                          <object class="GtkMenu" id="menu1">
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <child>
                              <object class="GtkImageMenuItem" id="menu_file_open">
                                <property name="label">gtk-open</property>
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="use_action_appearance">False</property>
                                <property name="use_underline">True</property>
                                <property name="use_stock">True</property>
                                <signal name="activate" handler="on_menu_file_open_activate" swapped="no"/>
                              </object>
                            </child>
                            <child>
                              <object class="GtkSeparatorMenuItem" id="separatormenuitem1">
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="use_action_appearance">False</property>
                              </object>
                            </child>
                            <child>
                              <object class="GtkImageMenuItem" id="menu_file_exit">
                                <property name="label">gtk-quit</property>
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="use_action_appearance">False</property>
                                <property name="use_underline">True</property>
                                <property name="use_stock">True</property>
                                <signal name="activate" handler="on_menu_file_exit_activate" swapped="no"/>
                              </object>
                            </child>
                          </object>
                        </child>
                      </object>
                    </child>
                    <child>
                      <object class="GtkMenuItem" id="menu_help">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_action_appearance">False</property>
                        <property name="label" translatable="yes">Aid_e</property>
                        <property name="use_underline">True</property>
                        <child type="submenu">
                          <object class="GtkMenu" id="menu3">
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <child>
                              <object class="GtkImageMenuItem" id="menu_help_about">
                                <property name="label">gtk-about</property>
                                <property name="visible">True</property>
                                <property name="can_focus">False</property>
                                <property name="use_action_appearance">False</property>
                                <property name="use_underline">True</property>
                                <property name="use_stock">True</property>
                              </object>
                            </child>
                          </object>
                        </child>
                      </object>
                    </child>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <object class="GtkHBox" id="hbox1">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkLabel" id="lbl_file">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="label" translatable="yes">Fichier</property>
                      </object>
                      <packing>
                        <property name="expand">True</property>
                        <property name="fill">True</property>
                        <property name="position">0</property>
                      </packing>
                    </child>
                    <child>
                      <object class="GtkEntry" id="txt_file">
                        <property name="visible">True</property>
                        <property name="can_focus">True</property>
                        <property name="invisible_char">•</property>
                        <property name="primary_icon_activatable">False</property>
                        <property name="secondary_icon_activatable">False</property>
                        <property name="primary_icon_sensitive">True</property>
                        <property name="secondary_icon_sensitive">True</property>
                      </object>
                      <packing>
                        <property name="expand">True</property>
                        <property name="fill">True</property>
                        <property name="position">1</property>
                      </packing>
                    </child>
                  </object>
                  <packing>
                    <property name="expand">True</property>
                    <property name="fill">True</property>
                    <property name="position">1</property>
                  </packing>
                </child>
                <child>
                  <object class="GtkHButtonBox" id="hbuttonbox">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="layout_style">end</property>
                    <child>
                      <object class="GtkButton" id="btn_exit">
                        <property name="label" translatable="yes">Quitter</property>
                        <property name="visible">True</property>
                        <property name="can_focus">True</property>
                        <property name="receives_default">True</property>
                        <property name="use_action_appearance">False</property>
                        <signal name="clicked" handler="on_btn_exit_clicked" swapped="no"/>
                      </object>
                      <packing>
                        <property name="expand">False</property>
                        <property name="fill">False</property>
                        <property name="position">0</property>
                      </packing>
                    </child>
                    <child>
                      <object class="GtkButton" id="btn_open">
                        <property name="label" translatable="yes">Ouvrir</property>
                        <property name="visible">True</property>
                        <property name="can_focus">True</property>
                        <property name="receives_default">True</property>
                        <property name="use_action_appearance">False</property>
                        <signal name="clicked" handler="on_btn_open_clicked" swapped="no"/>
                      </object>
                      <packing>
                        <property name="expand">False</property>
                        <property name="fill">False</property>
                        <property name="position">1</property>
                      </packing>
                    </child>
                  </object>
                  <packing>
                    <property name="expand">True</property>
                    <property name="fill">True</property>
                    <property name="position">2</property>
                  </packing>
                </child>
              </object>
            </child>
          </object>
        </interface>
        
        • Partager sur Facebook
        • Partager sur Twitter

        [PyGTK] Problème lors de la fermeture d'un filechooserdialog

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown