【问题标题】:Python GTK Color Chooser Widget - Set ColorPython GTK 颜色选择器小部件 - 设置颜色
【发布时间】:2015-03-01 05:40:29
【问题描述】:

我正在使用 Python3 和 GTK3(通过 gi.repository)编写程序。当在输入框中键入 RGB 值并单击“转换”时,我希望颜色选择器更改其选定的颜色。 “set_rgba()”命令(位于http://learngtk.org/tutorials/python_gtk3_tutorial/html/colorchooser.html)不会改变选定的颜色。没有生成错误消息(我从终端执行 Python 脚本)。

该文件包含每个按钮的功能,因此我将包含相关的sn-ps代码。

class ColorWin():
    """Color Dialog"""
    def __init__(self):
        self.ui = Gtk.Builder()
        self.ui.add_from_string(buffer=_GCOLOR)
        global _cc
        global _entry_rgb, _entry_hsi, _entry_hsl
        global _entry_hsv, _entry_cmyk, _entry_yiq
        _cc = self.ui.get_object('cc')
        _entry_rgb = self.ui.get_object('entry_rgb')
        _entry_hsi = self.ui.get_object('entry_hsi')
        _entry_hsl = self.ui.get_object('entry_hsl')
        _entry_hsv = self.ui.get_object('entry_hsv')
        _entry_cmyk = self.ui.get_object('entry_cmyk')
        _entry_yiq = self.ui.get_object('entry_yiq')
        # Match signal to function (handler)
        dic = {
            '_winexit' : Gtk.main_quit,
            '_submit_color': self._submit_color,
            'conv_color': self.conv_color,
            'conv_rgb': self.conv_rgb,
            'conv_hsi': self.conv_hsi,
            'conv_hsl': self.conv_hsl,
            'conv_hsv': self.conv_hsv,
            'conv_cmyk': self.conv_cmyk,
            'conv_yiq': self.conv_yiq,
        }
        self.ui.connect_signals(dic)

转换按钮的功能

def conv_rgb(self, _entry_rgb):
    """Convert RGB to *"""
    _rgb = _entry_rgb.get_text()
    _round = 6
    _red = re.sub('\(([0-9.]+), ([0-9.]+), ([0-9.]+)\)', r'\1', _rgb)
    _green = re.sub('\(([0-9.]+), ([0-9.]+), ([0-9.]+)\)', r'\2', _rgb)
    _blue = re.sub('\(([0-9.]+), ([0-9.]+), ([0-9.]+)\)', r'\3', _rgb)
    _red = round(float(_red), _round)
    _green = round(float(_green), _round)
    _blue = round(float(_blue), _round)
    _rgba_gdk = Gdk.RGBA(_red, _green, _blue, 1.000)
    _cc.set_rgba(_rgba_gdk)

我使用打印函数将每个变量的值打印到终端。我已经验证了值和数据类型是正确的。 “_rgba_gdk”是一个 Gdk.RGBA 对象(应该是这样)。 “_cc”是颜色选择器。我可以使用 _cc.get_rgba() 来获取当前选择的值。 但是,我想将它(通过 _cc.set_rgba(_rgba_gdk))更改为 RGB 输入框中的值(从 _entry_rgb.get_text() 获得)。 这将允许用户看到相关的颜色使用键入的 RGB 值(如果未指定 alpha,则假定 alpha 为 1)。

【问题讨论】:

    标签: python colors gtk3 pygobject


    【解决方案1】:

    问题似乎是get/set_rgba() 在小部件“样本”模式下使用当前选定的颜色,而不是在编辑器模式 (show-editor=True) 中使用。在编辑器模式下,更改编辑器也会更新当前颜色,但数据绑定不是双向的。我所能提供的只是一个技巧,它会在设置新颜色后强制编辑器更新:

    from gi.repository import Gtk
    from gi.repository import Gdk
    
    window = Gtk.Window()
    window.connect("destroy", Gtk.main_quit)
    
    box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
    window.add(box)
    
    colorchooser = Gtk.ColorChooserWidget(show_editor=True)
    box.add(colorchooser)
    
    entry = Gtk.Entry(text='0.5, 0.5, 0.5, 1.0')
    box.add(entry)
    
    def on_button_clicked(button):
        values = [float(v) for v in entry.get_text().split(',')]
        colorchooser.set_rgba(Gdk.RGBA(*values))
        colorchooser.set_property("show-editor", True)
    
    button = Gtk.Button(label="Parse RGBA")
    button.connect("clicked", on_button_clicked)
    box.add(button)
    
    window.show_all()
    Gtk.main()
    

    注意按钮点击回调中的colorchooser.set_property("show-editor", True)。这可能适用于所有版本的 GTK+,也可能不适用。如果调用set_rgba(),我会将其记录为请求更新颜色编辑器模式的错误: https://bugzilla.gnome.org/enter_bug.cgi?product=gtk%2B

    【讨论】:

    • 谢谢! "_cc.set_property("show-editor", True)" 完全按照我的意愿进行。 (顺便问一下,你是如何学习 PyGI 的?)
    • 附带说明一下,使用起来感觉更像 Python(至少对我而言):colorchooser.props.show_editor = True
    猜你喜欢
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多