繁体   English   中英

Python GTK颜色选择器小部件-设置颜色

[英]Python GTK Color Chooser Widget - Set Color

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

我的程序

该文件包含每个按钮的功能,因此我将包括相关的代码片段。

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)。

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

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 : set_rgba()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM