PyGtk3,可选标签上的隐藏光标

时间:2017-04-17 13:10:03

标签: pygtk

我可以使用set_selectable方法在pygtk3中创建可选标签。但我无法隐藏可选标签的光标。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

首先,您通常可以重新考虑禁用光标。没有它,您无法轻松选择(部分)文本。如果您只想要整个文本,则可能更容易label.get_text()来获取文本。

如果由于某些不明原因,您想要更改(或隐藏)光标,您可以使用窗口小部件的enterleave事件。标签小部件没有这些小部件,因此您必须先将GtkLabel插入GtkEventBox。以下是如何做到这一点:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  test_selectable_label.py
#
#  Copyright 2017 John Coppens <john@jcoppens.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#


from gi.repository import Gtk

class TestWindow(Gtk.VBox):
    def __init__(self):
        super(TestWindow, self).__init__()
        self.set_spacing(5)

        sel_label = Gtk.Label("abcdefghijklmnopqrstuvwxyz")
        sel_label.set_selectable(True)

        sel_evbox = Gtk.EventBox()
        sel_evbox.add(sel_label)
        sel_evbox.connect("enter_notify_event", self.enter_label)
        sel_evbox.connect("leave_notify_event", self.leave_label)
        self.pack_start(sel_evbox, False, False, 0)

        label = Gtk.Label()
        self.pack_start(label, False, False, 0)

    def enter_label(self, wdg, event):
        print("Entering")

    def leave_label(self, wdg, event):
        print("Leaving")


class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.connect("destroy", lambda x: Gtk.main_quit())

        self.add(TestWindow())
        self.show_all()

    def run(self):
        Gtk.main()


def main(args):
    mainwdw = MainWindow()
    mainwdw.run()

    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

我没有实现光标的实际隐藏/显示。一种方法是使其透明(请参阅override_cursor的{​​{1}}方法。GtkWidget将是另一种方法。