我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")
感谢由您的帮助