gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:54:28 28 4
gpt4 key购买 nike

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

My program

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

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 )。在编辑器模式下,更改编辑器也会更新当前颜色,但数据绑定(bind)不是双向的。我所能提供的只是一个技巧,它强制编辑器在设置新颜色后进行更新:

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

关于Python GTK 颜色选择器小部件 - 设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27749808/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com