2017-04-17 68 views
1

我可以用set_selectable方法在pygtk3中制作一个可选标签。但我无法隐藏可选标签的光标。有没有办法做到这一点?PyGtk3,在可选标签上隐藏光标

+0

,你将拥有一个单独的帐户,你在问一个问题,每一个类别?:) –

+0

@MadPhysicist,没有我喜欢的PyGTK。我在这里注册的目的通常是问一个关于PyGtk的问题。感谢您的编辑 – PyGtk3

+0

没有人知道解决方案?请帮助我。谢谢 – PyGtk3

回答

0

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

如果由于某种模糊的原因,你想改变(或隐藏)光标,你可以使用一个小部件的enterleave事件。标签小部件没有这些,因此您必须首先将GtkLabel插入GtkEventBox。以下是如何做到这一点:

#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 
# 
# test_selectable_label.py 
# 
# Copyright 2017 John Coppens <[email protected]> 
# 
# 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将是另一个。

+0

感谢您的评论。但它不起作用。看到这张图片https://drive.google.com/file/d/0ByGGW28GJgmoVHJjV2hBa3FrLVE/view?usp=sharing – PyGtk3

+0

您是否完成了程序中的enter_label和leave_label方法? Asi,程序只检测标签的进入和退出。 (有关override_cursor和set_style,请参阅下面的注释, – jcoppens

+0

我无法完成这些功能,因为我不知道它是如何完成的 – PyGtk3