在Gtk3中动态设置TreeView中Row的Selected属性(Python) [英] Setting Selected property of Row in TreeView dynamically in Gtk3 (python)

查看:154
本文介绍了在Gtk3中动态设置TreeView中Row的Selected属性(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我以前的问题中的问题相同,但是我移至python3/gtk3以便能够使用CSS设置基本属性.

This is the same problem as in my previous question, but I moved to python3/gtk3 to be able to use a css for setting the basic properties.

从python文件中:

From the python file:

self.w = Gtk.Window()
self.w.set_name("App")

我可以使用CSS:

#App GtkTreeView row:selected {
    border-color: #000000;
    border-top-width: 1px;
    border-bottom-width: 1px;
    color: #000;
}

轻松地永久更改所选内容的样式.对我来说,这意味着我应该能够动态访问row-对象及其样式,在其中可以为Gtk.StateFlags.SELECTED设置bg.

And easily permanently change the style of the selection. To me this means that I should be able to dynamically get access to the row-object and its style where I could set the bg for the Gtk.StateFlags.SELECTED.

我尝试了很多奇怪的方法,例如(其中bg_colorGdk.Color,可以很好地用于例如在TreeView之外更改Label的样式).

I've tried a bunch of weird ways, e.g (where bg_color is a Gdk.Color that works fine for e.g. changing the style of a Label outside the TreeView).

        style=self.treeview.get_style_context()
        col = style.get_background_color(Gtk.StateFlags.SELECTED)
        col.alpha =  1.0
        col.blue = bg_color.blue
        col.red = bg_color.red
        col.green = bg_color.green

或者:

        style = self.treeview.get_style().copy()
        style.bg[Gtk.StateFlags.SELECTED] = bg_color
        self.treeview.set_style(style)

(产生错误:style.bg[Gtk.StateFlags.SELECTED] = bg_color IndexError: list assignment index out of range)

(produces error: style.bg[Gtk.StateFlags.SELECTED] = bg_color IndexError: list assignment index out of range)

等...

所以,请问如何找到一种根据行的正常颜色动态更改选择效果的方法?或者换句话说,我如何找到真正拥有选择样式设置的对象呢?

So please, how do I find the way to dynamically change the selection effect depending on the normal-color of the row? Or in other words, how do I find my way to the object that actually holds the style-setting for the selection?

推荐答案

我对发布后最终完成的工作有最后的想法:

I had one last idea about how it could be done after posting which actually ended up working:

动态重新加载CSS:

在CSS中,我添加了一行,使背景值可以动态替换:

In the css I added a row leaving the value for the background open to dynamic substitution:

#App GtkTreeView row:selected {
    border-color: #400;
    border-top-width: 2px;
    border-bottom-width: 2px;
    background: {0};
    color: #000;
}

然后我将css加载到python中:

Then I loaded the css in python:

    screen = Gdk.Screen.get_default()
    self._css_provider = Gtk.CssProvider()
    css = open("notify_stack.css", 'rb')
    self._css = css.read()
    css.close()
    self._css_from = bytes("{0}".encode("utf8"))
    self._css_provider.load_from_data(self._css.replace(
        self._css_from,   
        bytes("#fff".encode("utf8"))))
    context = Gtk.StyleContext()
    context.add_provider_for_screen(screen, self._css_provider,
        Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

稍后在回调中选择行时,我将这一行(data是我的ListStore)放在这里:

Later in the callback for when a row is selected I put this row (data is my ListStore):

        self._css_provider.load_from_data(
            self._css.replace(self._css_from,
                bytes(data[rows[0]][self.BG_COLOR].encode("utf8"))))

感觉真的很残酷,必须有更好的方法,但是嘿,它确实有效.

It feels really brute, there must be a nicer way, but hey it actually worked.

这篇关于在Gtk3中动态设置TreeView中Row的Selected属性(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆