我正在尝试(失败)使用Python 3和Gtk设置进度条的颜色。我已经尝试过Ubuntu 16.04(Python 3.5和Gtk 3.18.9)和18.04(Python 3.6和Gtk 3.22.30)。下面的代码直接从Gtk教程中提取,并从GtkProgressBar with CSS for progress colour not functioning中添加了一小段css。
以下两张图片显示了我的意思。这些是同一运行程序的2个屏幕截图。我要做的就是在终端窗口和gtk窗口之间单击。
在Ubuntu 18.04上,除非我将焦点从Gtk窗口移开,否则进度条的颜色不会变为绿色。下图显示了我的意思-焦点位于终端窗口上。
在下面的图像中,焦点位于Gtk窗口上,进度条的颜色仍为默认颜色-橙色。
在Ubuntu 16.04上,进度条的颜色永远不会变为绿色。
这是Gtk中的错误,还是我遗漏了一些东西?
'''example from https://python-gtk-3-tutorial.readthedocs.io/en/latest/progressbar.html'''
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib, Gdk
print("Gtk Version Information: major:{0}, minor:{1}, micro: {2}".format(Gtk.MAJOR_VERSION, Gtk.MINOR_VERSION, Gtk.MICRO_VERSION))
class ProgressBarWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ProgressBar Demo")
self.set_border_width(10)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(vbox)
self.progressbar = Gtk.ProgressBar()
vbox.pack_start(self.progressbar, True, True, 0)
button = Gtk.CheckButton("Show text")
button.connect("toggled", self.on_show_text_toggled)
vbox.pack_start(button, True, True, 0)
button = Gtk.CheckButton("Activity mode")
button.connect("toggled", self.on_activity_mode_toggled)
vbox.pack_start(button, True, True, 0)
button = Gtk.CheckButton("Right to Left")
button.connect("toggled", self.on_right_to_left_toggled)
vbox.pack_start(button, True, True, 0)
self.timeout_id = GLib.timeout_add(50, self.on_timeout, None)
self.activity_mode = False
#css code from https://stackoverflow.com/questions/48097764/gtkprogressbar-with-css-for-progress-colour-not-functioning
css = b'''
progressbar > trough > progress {
background-color: green;
}
'''
css_provider = Gtk.CssProvider()
css_provider.load_from_data(css)
context = Gtk.StyleContext()
screen = Gdk.Screen.get_default()
context.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
def on_show_text_toggled(self, button):
show_text = button.get_active()
if show_text:
text = "some text"
else:
text = None
self.progressbar.set_text(text)
self.progressbar.set_show_text(show_text)
def on_activity_mode_toggled(self, button):
self.activity_mode = button.get_active()
if self.activity_mode:
self.progressbar.pulse()
else:
self.progressbar.set_fraction(0.0)
def on_right_to_left_toggled(self, button):
value = button.get_active()
self.progressbar.set_inverted(value)
def on_timeout(self, user_data):
"""
Update value on the progress bar
"""
if self.activity_mode:
self.progressbar.pulse()
else:
new_value = self.progressbar.get_fraction() + 0.01
if new_value > 1:
new_value = 0
self.progressbar.set_fraction(new_value)
# As this is a timeout function, return True so that it
# continues to get called
return True
win = ProgressBarWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
答案 0 :(得分:0)
您的示例很好,只需将CSS代码段更改为:
'''
progressbar > trough > progress {
background-image: none;
background-color: green;
}
'''
Ubuntu Ambience主题可能使用background-image: linear-gradient()
来设置进度条样式。