Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Gtk进度条颜色_Python 3.x_Gtk3 - Fatal编程技术网

Python 3.x Gtk进度条颜色

Python 3.x Gtk进度条颜色,python-3.x,gtk3,Python 3.x,Gtk3,我正在尝试(但失败)使用Python3和Gtk设置进度条的颜色。我在Ubuntu 16.04(Python 3.5和Gtk 3.18.9)和18.04(Python 3.6和Gtk 3.22.30)上都试过。下面的代码直接来自Gtk教程,其中添加了一小段css 下面的两张图片显示了我的意思。这是同一运行程序的两个屏幕截图。我所做的就是在终端窗口和gtk窗口之间单击 在Ubuntu18.04上,进度条的颜色不会变为绿色,除非我将焦点从Gtk窗口移开。下图显示了我的意思-焦点在终端窗口上。 在下图

我正在尝试(但失败)使用Python3和Gtk设置进度条的颜色。我在Ubuntu 16.04(Python 3.5和Gtk 3.18.9)和18.04(Python 3.6和Gtk 3.22.30)上都试过。下面的代码直接来自Gtk教程,其中添加了一小段css

下面的两张图片显示了我的意思。这是同一运行程序的两个屏幕截图。我所做的就是在终端窗口和gtk窗口之间单击

在Ubuntu18.04上,进度条的颜色不会变为绿色,除非我将焦点从Gtk窗口移开。下图显示了我的意思-焦点在终端窗口上。

在下图中,焦点在Gtk窗口上,进度条颜色保持默认颜色-橙色

在Ubuntu 16.04上,进度条的颜色永远不会变为绿色

这是Gtk中的一个bug,还是我遗漏了什么?

'''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()

您的示例没有问题,只需将CSS片段更改为:

'''
progressbar > trough > progress {
  background-image: none;
  background-color: green;
}
'''

Ubuntu环境主题可能使用了
背景图像:linear-gradient()
来设置进度条的样式。

非常感谢!这很好用。你怎么知道的?我在任何地方都找不到这个文档!起初,我也曾为此挣扎:)