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



i可以使用set_selectable方法在pygtk3中制作一个可选的标签。但是我无法隐藏可选标签的光标。是否有一种方法可以执行此操作?

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

如果出于某种晦涩的原因,您 do 想要更改(或隐藏(光标,则可以使用窗口小部件的enterleave事件。标签小部件没有这些,因此您必须首先将GtkLabel插入CC_5。这是这样做的方法:

#!/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))

我没有实现光标的实际隐藏/显示。一种方法是使其透明(请参阅GtkWidgetoverride_cursor方法。set_style将是另一个。

最新更新