我的一天的问题是关于在Gtk.MessageDialog中的默认按钮中应用CSS。我尝试了很多没有结果的东西。
这笔交易是为了找到像按钮,GTK按钮,或GtkMessageDialog.Button这样的好身份......,
#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-
#demo_messagedialog_css.py
from gi.repository import Gtk,Gdk
class show_message_dlg:
def __init__(self, message, type_message=Gtk.MessageType.INFO,stock_message=Gtk.STOCK_DIALOG_INFO, decorate=True):
"""
This Function is used to show an message
error dialog when an error occurs.
error_string - The error string that will be displayed on the dialog.
==>type_message=gtk.MESSAGE_ERROR for error message
==>type_message=gtk.MESSAGE_INFO for information message
==>type_message=gtk.MESSAGE_WARNING for warning message
GTK_WIN_POS_NONE
GTK_WIN_POS_CENTER equivalent in python to Gtk.WindowPosition.CENTER
GTK_WIN_POS_MOUSE equivalent in python to Gtk.WindowPosition.MOUSE
GTK_WIN_POS_CENTER_ALWAYS equivalent in python to Gtk.WindowPosition.CENTER_ALWAYS
GTK_WIN_POS_CENTER_ON_PARENT equivalent in python to Gtk.WindowPosition.CENTER_ON_PARENT
"""
self.message = message
self.message_dlg = Gtk.MessageDialog(type = type_message
, buttons = Gtk.ButtonsType.OK)
self.message_dlg.set_decorated(decorate)
self.message_dlg.set_markup(self.message)
self.message_dlg.set_position(Gtk.WindowPosition.CENTER_ON_PARENT )
style_provider = Gtk.CssProvider()
css = """
GtkMessageDialog
{ background:linear-gradient(to bottom, green, rgba(0,255,0,0));}
#Buttons{ background-color: yellow}
"""
style_provider.load_from_data(css)
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
def run(self):
reponse = self.message_dlg.run()
self.message_dlg.destroy()
return reponse
if __name__ == "__main__":
exemple = show_message_dlg(u"message in the box dialog ")
exemple.run()
Gtk.main()
经过几个白夜和150黑咖啡后,我找到了一小部分答案
#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-
#demo_messagedialog_css1.py
from gi.repository import Gtk,Gdk
class MyButtonClass(Gtk.Button):
__gtype_name__ = 'MyButton'
def __init__(self, label):
Gtk.Button.__init__(self, label)
self.connect("clicked", self._clicked1)
def _clicked1(self, button):
print "button ok clicked"
class show_message_dlg:
def __init__(self, message, type_message=Gtk.MessageType.INFO,stock_message=Gtk.STOCK_DIALOG_INFO, decorate=True):
self.message = message
self.message_dlg = Gtk.MessageDialog(type = type_message)
self.message_dlg.set_decorated(decorate)
self.message_dlg.set_markup(self.message)
self.message_dlg.set_position(Gtk.WindowPosition.CENTER_ALWAYS )
button_v = MyButtonClass('Ok button')
self.message_dlg.add_action_widget(button_v, Gtk.ResponseType.OK)
self.message_dlg.set_default_response(Gtk.ResponseType.OK)
# line below necessary if button not defined like Gtk.MessageDialog(type = type_message, buttons = Gtk.ButtonsType.OK)
self.message_dlg.show_all()
style_provider = Gtk.CssProvider()
css = """
GtkMessageDialog
{ background:linear-gradient(to bottom, yellow, rgba(0,255,0,0));}
MyButton {
color: darkgrey;
font: Comic Sans 20;} /* run OK for label font */
MyButton GtkLabel{ background-color: blue} /* run OK for button background label*/
/*MyButton GtkLabel{ background:linear-gradient(to right, yellow, blue,yellow,green,red,orange);}*/ /* run OK for button background label*/
MyButton:active GtkLabel{ background-color: red;} /* do not run if state change */
"""
style_provider.load_from_data(css)
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
def run(self):
reponse = self.message_dlg.run()
self.message_dlg.destroy()
return reponse
if __name__ == "__main__":
exemple = show_message_dlg(u"message in the dialog box ")
response = exemple.run()
if response == Gtk.ResponseType.OK:
print("OK button clicked and end")
else:
print("destroyed")
Gtk.main()
请注意特殊课程按钮,课程类别为 gtype_name =' MyButton'和MyButton {}
处理的CSS命令现在,当我点击按钮
时,我不明白为什么下面部分不正常MyButton:active GtkLabel{ background-color: red;}
答案 0 :(得分:0)
好的,我找到了特定问题按钮的解决方案
MyButton:active GtkLabel{ background: red;}
我的例子
#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-
#demo_messagedialog_css2.py
from gi.repository import Gtk,Gdk
class MyButtonClass(Gtk.Button):
__gtype_name__ = 'MyButton'
def __init__(self, label):
Gtk.Button.__init__(self, label)
self.connect("clicked", self._clicked1)
def _clicked1(self, button):
print "button ok clicked"
class show_message_dlg:
def __init__(self, message, type_message=Gtk.MessageType.INFO,stock_message=Gtk.STOCK_DIALOG_INFO, decorate=True):
self.message = message
self.message_dlg = Gtk.MessageDialog(type = type_message)
self.message_dlg.set_decorated(decorate)
self.message_dlg.set_markup(self.message)
self.message_dlg.set_position(Gtk.WindowPosition.CENTER_ALWAYS )
button_v = MyButtonClass('Ok button')
self.message_dlg.add_action_widget(button_v, Gtk.ResponseType.OK)
self.message_dlg.set_default_response(Gtk.ResponseType.OK)
# line below necessary if button not defined like Gtk.MessageDialog(type = type_message, buttons = Gtk.ButtonsType.OK)
self.message_dlg.show_all()
style_provider = Gtk.CssProvider()
css = """
GtkMessageDialog
{ background:linear-gradient(to bottom, yellow, rgba(0,255,0,0));}
MyButton {
color: darkgrey;
font: Comic Sans 20;}
MyButton:active { background: red;}
MyButton GtkLabel{ background:linear-gradient(to right, yellow, blue,yellow,green,red,orange);}
"""
style_provider.load_from_data(css)
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
def run(self):
reponse = self.message_dlg.run()
self.message_dlg.destroy()
return reponse
if __name__ == "__main__":
exemple = show_message_dlg(u"message in the dialog box ")
response = exemple.run()
if response == Gtk.ResponseType.OK:
print("OK button clicked and end")
else:
print("destroyed")
Gtk.main()