PyGTK Hide Cursor

丶灬走出姿态 提交于 2019-12-11 04:03:48

问题


The question is simple how can I hide the cursor on an active window using PyGTK???

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. I got this error.

AttributeError: 'gtk.Window' object has no attribute 'set_cursor'

After seeing that error I realized gt.Window won't be able to do it, but gtk.gdk.Window will. However how can I convert this basic window so it'll hide the cursor.


回答1:


As stated in the PyGTK FAQ, you should set the cursor on the realize signal. 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.

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()


来源:https://stackoverflow.com/questions/6718586/pygtk-hide-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!