2016-04-24 97 views
1

我loocking任何想法解决我的问题如何设置按钮的默认响应与add_action_widget在Gtk.Dialog

请参见后续代码。 这是一个演示,以解释什么是关于 事实上,我需要使用add_action_widget来添加按钮,以添加图像的按钮。如果我混合使用add_action_widget和add_button小部件的纵向大小不一样而且很丑陋。

我想只使用add_action_widget并能够设置有效的按钮默认响应

#!/usr/bin/env python 
# coding: utf-8 
#dialog_defaultbutton.py 
from gi.repository import Gtk 

class boite_stockicon_etiquette: 
    ''' box with STOCK Icon ''' 
    def __init__(self,text, stock_icon=None,sizeicon=Gtk.IconSize.MENU): 
     # On cree une boite pour la pixmap et l'etiquette 
     self.box = Gtk.HBox(False, 0) 
     self.box.set_border_width(2) 
     # A present l'image. 
     if stock_icon is not None: 
      image = Gtk.Image.new_from_stock(stock_id=stock_icon,size=sizeicon)    
     else: 
      image = Gtk.Image.new_from_stock(stock_id=Gtk.STOCK_OK,size=sizeicon)     
     # On cree une etiquette pour le bouton. 
     etiquette = Gtk.Label(text) 
     # pack image and label in the box 
     self.box.pack_start(image, False, False, 3) 
     self.box.pack_start(etiquette, False, False, 3) 
     """image.show() 
     etiquette.show()""" 

    def box_xpm(self): 
     return self.box 

class dialogDefaultButton: 
    """ make button with stock icon""" 
    def _make_button(self,etiquette=None,stock_icon=None): 
     boite1= boite_stockicon_etiquette(etiquette,stock_icon=stock_icon) 
     boite = boite1.box_xpm() 
     bouton = Gtk.Button()  
     bouton.add(boite)  
     return bouton  

    def __init__(self,text): 
     self.dialog = Gtk.Dialog(title="Dialog") 

     self.dialog.set_default_size(400, 300) 
     self.dialog.set_border_width(10) 

     self.dialog.add_action_widget(self._make_button(etiquette=u'Valid',stock_icon=Gtk.STOCK_OK),Gtk.ResponseType.OK) 
     self.dialog.add_action_widget(self._make_button(etiquette=u'Cancel',stock_icon=Gtk.STOCK_CANCEL),Gtk.ResponseType.CANCEL) 

     #Please test under with True ou False 
     flag_button_valide_setdefault = False 

     #if flag_button_valide_setdefault is True the valid button is set like self.dialog.set_default_response(Gtk.ResponseType.OK) 
     if flag_button_valide_setdefault is not True: 
      self.dialog.add_action_widget(self._make_button(etiquette=u'Add', stock_icon=Gtk.STOCK_ADD),Gtk.ResponseType.APPLY) 
      self.dialog.add_action_widget(self._make_button(etiquette=u'Help', stock_icon=Gtk.STOCK_ABOUT),Gtk.ResponseType.HELP) 

     label = Gtk.Label(text) 

     content_area = self.dialog.get_content_area() 
     content_area.add(label) 
     self.dialog.show_all() 
     #Here action do not run ok why ? 
     # Ok only with button created with add_button 



    def run(self): 
     while 1: 
      self.reponse = self.dialog.run() 
      if self.reponse in [Gtk.ResponseType.OK, Gtk.ResponseType.CANCEL,Gtk.ResponseType.DELETE_EVENT]: 
       print("OK sa marche button clicked") 
       print self.reponse 
       break 
      else: 
       print 'Hello' 

     self.dialog.destroy()   
     return self.reponse 

if __name__ == "__main__": 
    demo = dialogDefaultButton(text= u'demo of default button depend widget list why?\n\ 
\n\ 
if flag_button_valide_setdefault == True , \n\ 
the valid button is set like self.dialog.set_default_response(Gtk.ResponseType.OK)\n\ 
if flag_button_valide_setdefault == False \n\ 
\n\ 
if somebody could help me to understand what is about !!!!!\n\ 
in fact I need to have flag_button_valide_setdefault =False\n\ 
and the valid button set with default response like case of flag==True') 
    response = demo.run() 
    if response == Gtk.ResponseType.OK: 
     print("OK button clicked") 
    elif response == Gtk.ResponseType.CANCEL: 
     print("Cancel button clicked") 
    else: 
     print("Dialog closed") 

感谢由您的帮助

回答

0

我发现要想在Gtk.dialog管理键式解决方案。 当我试图了解什么是与不同类型的Gtk.ResponseType有关。我发现有时不可能影响按钮,我想要的......它的小嫌疑犯:-) 事实上,经过一些测试,我发现这一点。当你制作一个Gtk.dialog,并且单独准备Gtk.Button,并且通过add_action_widget添加它时,按钮的制作顺序就很重要。还有答复的类型。 4规则遵循:

规则1:为了与像set_default_response(RESPONSE_ID)动作的按钮,你必须先声明这一点。

规则2:您可以使用任何类型的Gtk.ResponseType与任何股票图标。 Gtk不要链接它。尽管如此,链接一个标签='不好'和一个Gtk.ResponseType.YES的按钮可能会很奇怪!更好地保持标签和Gtk.ResponseType之间的一致性。

规则3: Gtk.ResponseType.HELP的例外。与self.dialog.add_action_widget(bouton_help,Gtk.ResponseType.HELP)制成的按钮,如果你在最后一个动作使其设定firt留下相同且其设置默认响应

规则N°4:如果你想要使用帮助按钮,并保持其他确定按钮设置默认响应, 使其正常第一。之后,制作一个类似按钮的按钮 button_help = my_method(label_help,stock_icon = Gtk.STOCK_HELP),其中my_method正在制作带有股票图标的按钮,例如 将按钮添加到对话框...... self.dialog.add_action_widget (bouton_help,Gtk.ResponseType.NO)。 小心不要用Gtk.ResponseType.HELP而是利用Gtk.ResponseType.NO例如

#!/usr/bin/env python 
# coding: utf-8 
#test_dialog_defaultbutton.py 
from gi.repository import Gtk 

class boite_stockicon_etiquette: 
    ''' box with STOCK Icon ''' 
    def __init__(self,text, stock_icon=None,sizeicon=Gtk.IconSize.MENU): 
     # On cree une boite pour la pixmap et l'etiquette 
     self.box = Gtk.HBox(False, 0) 
     self.box.set_border_width(2) 
     # A present l'image. 
     if stock_icon is not None: 
      image = Gtk.Image.new_from_stock(stock_id=stock_icon,size=sizeicon)    
     else: 
      image = Gtk.Image.new_from_stock(stock_id=Gtk.STOCK_OK,size=sizeicon)     
     # On cree une etiquette pour le bouton. 
     etiquette = Gtk.Label(text) 
     # pack image and label in the box 
     self.box.pack_start(image, False, False, 3) 
     self.box.pack_start(etiquette, False, False, 3) 
     """image.show() 
     etiquette.show()""" 

    def box_xpm(self): 
     return self.box 

class dialogDefaultButton: 
    """ make button with stock icon""" 
    def _make_button(self,etiquette=None,stock_icon=None): 
     boite1= boite_stockicon_etiquette(etiquette,stock_icon=stock_icon) 
     boite = boite1.box_xpm() 
     bouton = Gtk.Button()  
     bouton.add(boite)  
     return bouton  

    def __init__(self,text): 
     self.dialog = Gtk.Dialog(title="Dialog") 

     self.dialog.set_default_size(400, 300) 
     self.dialog.set_border_width(10) 

     # code de test pour voir l'ordre des boutons dans le widget 
     self.dialog.add_action_widget(self._make_button(etiquette=u'Ok(-5)',stock_icon=Gtk.STOCK_OK),Gtk.ResponseType.OK) #indice -5 
     self.dialog.add_action_widget(self._make_button(etiquette=u'Cancel(-6)',stock_icon=Gtk.STOCK_CANCEL),Gtk.ResponseType.CANCEL) #indice -6 
     self.dialog.add_action_widget(self._make_button(etiquette=u'close(-7)', stock_icon=Gtk.STOCK_ABOUT),Gtk.ResponseType.CLOSE)#indice -7 
     self.dialog.add_action_widget(self._make_button(etiquette=u'YES(-8)', stock_icon=Gtk.STOCK_ADD),Gtk.ResponseType.YES)#indice -8 
     self.dialog.add_action_widget(self._make_button(etiquette=u'NON(-9)', stock_icon=Gtk.STOCK_ADD),Gtk.ResponseType.NO)#indice -9 
     self.dialog.add_action_widget(self._make_button(etiquette=u'APPLY(-10)', stock_icon=Gtk.STOCK_ADD),Gtk.ResponseType.APPLY)#indice -10 
     # do not use in case of need like these self.info_dlg.set_default_response(Gtk.ResponseType.OK) 
     # please check line below with # or not 
     self.dialog.add_action_widget(self._make_button(etiquette=u'HELP(-11)', stock_icon=Gtk.STOCK_ADD),Gtk.ResponseType.HELP)#indice -11 
     """ 
     # three next button run ok also Gtk doc may be not update :-) 
     self.dialog.add_action_widget(self._make_button(etiquette=u'ACCEPT(-3)', stock_icon=Gtk.STOCK_ADD),Gtk.ResponseType.ACCEPT)#indice -3 
     self.dialog.add_action_widget(self._make_button(etiquette=u'REJECT(-2)', stock_icon=Gtk.STOCK_ADD),Gtk.ResponseType.REJECT)#indice -2 
     self.dialog.add_action_widget(self._make_button(etiquette=u'FONCTION(42)', stock_icon=Gtk.STOCK_ADD),42)#indice -2 
     """   
     label = Gtk.Label(text) 

     content_area = self.dialog.get_content_area() 
     content_area.add(label) 
     self.dialog.show_all() 

    def run(self): 
     while 1: 
      self.reponse = self.dialog.run() 
      if self.reponse in [Gtk.ResponseType.OK, Gtk.ResponseType.CANCEL,Gtk.ResponseType.DELETE_EVENT]: 
       print("run OK button clicked (sa marche)") 
       print "Gtk.ResponseType is ", self.reponse 
       break 
      else: 
       print 'Hello' 
       print "Gtk.ResponseType is ", self.reponse 

     self.dialog.destroy()   
     return self.reponse 

if __name__ == "__main__": 
    demo = dialogDefaultButton(text= u'demo de bouton pour montrer que tout bouton positionné avec un signal de type\n\ 
Gtk.ResponseType.HELP va avoir une position isolée des autres boutons et même gardera un écart.\n\ 
\n\ 
Demo in order to show how manage order button in Gtk.dialog.\n\ 
Gtk.ResponseType.HELP "give an order" to put help button on the left et the default become help button ') 
    response = demo.run() 
    if response == Gtk.ResponseType.OK: 
     print("OK button clicked") 
    elif response == Gtk.ResponseType.CANCEL: 
     print("Cancel button clicked") 
    else: 
     print("Dialog closed")