Gtk 进度条颜色
Gtk Progress Bar Color
我正在尝试(但失败了)使用 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 个屏幕截图。我所做的就是在终端 window 和 gtk window.
之间点击
在 Ubuntu 18.04 上,进度条颜色不会变为绿色,除非我将焦点从 Gtk Window 上移开。下图显示了我的意思——焦点在终端 window 上。
下图中,焦点在Gtk上window,进度条颜色保持默认颜色-橙色。
在 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
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()
您的示例没问题,只需将 CSS 代码段更改为:
'''
progressbar > trough > progress {
background-image: none;
background-color: green;
}
'''
可能 Ubuntu Ambience 主题使用 background-image: linear-gradient()
来设置进度条的样式。
我正在尝试(但失败了)使用 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 个屏幕截图。我所做的就是在终端 window 和 gtk window.
之间点击在 Ubuntu 18.04 上,进度条颜色不会变为绿色,除非我将焦点从 Gtk Window 上移开。下图显示了我的意思——焦点在终端 window 上。
下图中,焦点在Gtk上window,进度条颜色保持默认颜色-橙色。
在 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
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()
您的示例没问题,只需将 CSS 代码段更改为:
'''
progressbar > trough > progress {
background-image: none;
background-color: green;
}
'''
可能 Ubuntu Ambience 主题使用 background-image: linear-gradient()
来设置进度条的样式。