PyGtk:修改gtk FileChooserButton的背景颜色/图像
我尝试了以下方法来改变文件选择按钮的背景颜色,但没有任何变化……
gtkrc 文件:
style "FilechooserButtonStyle" = "button_style"
{
base[NORMAL] = '#F5F5F5'
base[SELECTED] = 'red'
bg[NORMAL] = 'green'
bg_pixmap[NORMAL] = "button.jpg"
xthickness = 10
ythickness = 10
GtkFileChooserButton::set_app_paintable = True
}
在 Python 文件中:
file_chooser.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse("red"))
file_chooser.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color('gray'))
我该如何解决这个问题呢?
提前谢谢你!
1 个回答
1
我对.rc文件不是很熟悉,不过代码是这样工作的:
#!/usr/bin/env python
# example filechooser.py
import pygtk
pygtk.require('2.0')
import gtk
dialog = gtk.FileChooserDialog("Open..", None,
gtk.FILE_CHOOSER_ACTION_OPEN,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
dialog.set_default_response(gtk.RESPONSE_OK)
## here it is
##
for child in dialog.get_children():
for enf in child.get_children():
if isinstance(enf, gtk.HButtonBox):
for butt in enf.get_children():
style = butt.get_style().copy ()
style.bg[gtk.STATE_NORMAL] = butt.get_colormap().alloc (0xffff, 0x0000, 0x0000)
butt.set_style (style)
##
response = dialog.run()
if response == gtk.RESPONSE_OK:
print dialog.get_filename(), 'selected'
elif response == gtk.RESPONSE_CANCEL:
print 'Closed, no files selected'
dialog.destroy()
它会在所有对话框的子元素中搜索按钮,并把它们的样式改成红色。希望这对你有帮助!