减少Gtk + Python中CellRendererText中编辑框的大小

时间:2018-04-26 12:31:53

标签: python-2.7 gtk3 gnome

我正在尝试减少在Gtk + Python中编辑treeview单元格时出现的编辑框的大小。

这是我的代码:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class CellRendererTextWindow(Gtk.Window):

   def __init__(self):
        Gtk.Window.__init__(self, title="CellRendererText Example")

        self.set_default_size(200, 200)

        self.liststore = Gtk.ListStore(str, str)
        self.liststore.append(["Fedora", "http://fedoraproject.org/"])
        self.liststore.append(["Slackware", "http://www.slackware.com/"])
        self.liststore.append(["Sidux", "http://sidux.com/"])

        treeview = Gtk.TreeView(model=self.liststore)

        select_render = Gtk.CellRendererToggle()
        select_render.set_property('activatable', True)
        select_render.set_property("radio", True)
        select_render.connect('toggled', self.on_toggle)
        select_column = Gtk.TreeViewColumn(" %s" %('Select'), select_render,    
        active=0)
        select_column.set_clickable(True)
        treeview.append_column(select_column)

        renderer_text = Gtk.CellRendererText()
        renderer_text.set_property("editable", True)
        column_text = Gtk.TreeViewColumn("Text", renderer_text, text=0)
        treeview.append_column(column_text)

        renderer_editabletext = Gtk.CellRendererText()
        renderer_editabletext.set_property("editable", True)

        column_editabletext = Gtk.TreeViewColumn("Editable Text",
        renderer_editabletext, text=1)
        treeview.append_column(column_editabletext)

        renderer_editabletext.connect("edited", self.text_edited)

        self.add(treeview)

    def text_edited(self, widget, path, text):
        self.liststore[path][1] = text

    def on_toggle(self, cell, path):
        print "Toggled"

win = CellRendererTextWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

以下是此代码的输出:

output

当我尝试编辑单元格时,它显示如下:

edit cell

我想要的是,我希望减小编辑单元格时出现的编辑框的大小,因为它与行的其他单元格重叠,我希望该编辑框出现在其列宽内。注意我没有'我想按此顺序影响列或单元格大小。

我尝试设置各种属性,例如:widthwidth-charsmax-width-chars等。

max-width-chars显示了一些影响,但随后它也减小了列的大小。

之前它发生在gtk2中,但在Gtk3 +中它显示出这种影响。

有人对此有任何解决方案吗?我真的很困惑。

1 个答案:

答案 0 :(得分:0)

gtk单元格渲染器文本的编辑框有一些固定的宽度。

因此,当您使用一些空格分配每个列时,编辑框的重叠将不会发生。

在你的代码中,我简单地增加了窗口大小的宽度,这样,cellrendererText就可以获得足够的空间来打开编辑框而不会与其他框重叠

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class CellRendererTextWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="CellRendererText Example")

        self.set_default_size(400, 200)

        self.liststore = Gtk.ListStore(str, str)
        self.liststore.append(["Fedora", "http://fedoraproject.org/"])
        self.liststore.append(["Slackware", "http://www.slackware.com/"])
        self.liststore.append(["Sidux", "http://sidux.com/"])

        treeview = Gtk.TreeView(model=self.liststore)
        treeview.set_grid_lines(Gtk.TreeViewGridLines.BOTH)

        select_render = Gtk.CellRendererToggle()
        select_render.set_property('activatable', True)
        select_render.set_property("radio", True)
        select_render.connect('toggled', self.on_toggle)
        select_column = Gtk.TreeViewColumn(" %s" %('Select'), select_render, active=0)
        select_column.set_clickable(True)
        treeview.append_column(select_column)

        renderer_text = Gtk.CellRendererText()
        renderer_text.set_property("editable", True)
        column_text = Gtk.TreeViewColumn("Text", renderer_text, text=0)
        column_text.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
        column_text.set_expand(True)
        treeview.append_column(column_text)

        renderer_editabletext = Gtk.CellRendererText()
        renderer_editabletext.set_property("editable", True)

        column_editabletext = Gtk.TreeViewColumn("Editable Text", renderer_editabletext, text=1)
        treeview.append_column(column_editabletext)

        renderer_editabletext.connect("edited", self.text_edited)

        self.add(treeview)

    def text_edited(self, widget, path, text):
        self.liststore[path][1] = text

    def on_toggle(self, cell, path):
        print "Toggled"

win = CellRendererTextWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

Here is the output