简体   繁体   English

PyGTK 隐藏 Cursor

[英]PyGTK Hide Cursor

The question is simple how can I hide the cursor on an active window using PyGTK???问题很简单,如何使用 PyGTK 在活动的 window 上隐藏 cursor?

Here's a basic app I made to learn this...这是我为学习这个而制作的一个基本应用程序......

#!/usr/bin/env python

import gtk

class app:

  def __init__(self):
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.set_title("TestApp")
    window.set_default_size(400,200)
    pixmap = gtk.gdk.Pixmap(None, 1, 1, 1)
    color = gtk.gdk.Color()
    cursor = gtk.gdk.Cursor(pixmap, pixmap, color, color, 0, 0)
    window.set_cursor(cursor)
    window.connect("destroy", gtk.main_quit)    
    window.show_all()

app()
gtk.main()

Obviously all it is, is just a window, however when I went to try and run it.显然,它只是一个 window,但是当我尝试运行它时。 I got this error.我得到了这个错误。

AttributeError: 'gtk.Window' object has no attribute 'set_cursor' AttributeError: 'gtk.Window' object 没有属性 'set_cursor'

After seeing that error I realized gt.Window won't be able to do it, but gtk.gdk.Window will.看到该错误后,我意识到 gt.Window 将无法做到,但 gtk.gdk.Window 会。 However how can I convert this basic window so it'll hide the cursor.但是,我如何转换这个基本的 window 以便隐藏 cursor。

As stated in the PyGTK FAQ , you should set the cursor on the realize signal.PyGTK FAQ 中所述,您应该在realize信号上设置 cursor。 If you don't wait for the realize signal, the gtk.gdk.window hasn't been created yet , so you can't change the cursor.如果您不等待realize信号, gtk.gdk.window尚未创建,因此您无法更改 cursor。

So, you can do something like:因此,您可以执行以下操作:

#!/usr/bin/env python

import gtk

class app:

  def __init__(self):
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.set_title("TestApp")
    window.set_default_size(400,200)
    window.connect("realize", self.realize_cb)
    window.connect("destroy", gtk.main_quit)    
    window.show_all()

  def realize_cb(self, widget):
    pixmap = gtk.gdk.Pixmap(None, 1, 1, 1)
    color = gtk.gdk.Color()
    cursor = gtk.gdk.Cursor(pixmap, pixmap, color, color, 0, 0)
    widget.window.set_cursor(cursor)

app()
gtk.main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM