简体   繁体   English

在 Pygtk 应用程序中更改 Cursor 类型

[英]Change Cursor type in Pygtk Application

In my application I want to change cursor type of all buttons, on mouse leave and enter events.在我的应用程序中,我想在鼠标离开和输入事件时更改所有按钮的 cursor 类型。 for this, I want to write generalized code instead of connecting each button with appropriate signal.为此,我想编写通用代码,而不是用适当的信号连接每个按钮。

here's an example code for it.这是它的示例代码。

import gtk

class Button(gtk.Button):

   __gsignals__ = {
    "leave" : "override",
    "enter" : "override"
    }

   def do_leave(self):
      self.window.set_cursor(None)

   def do_enter(self):
      print "Enter"
      self.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND2))

class EventBox:

  def __init__(self):
    window = gtk.Window()
    vbox = gtk.VBox()
    label = gtk.Label("Change Cursor")
    vbox.pack_start(label, False, False)
    bt = gtk.Button('Ok')
    bt.connect('clicked', self.on_click, window)
    vbox.pack_start(bt, False, False)
    eventbox = gtk.EventBox()
    window.set_size_request(400,400)
    window.add(eventbox)
    eventbox.add(vbox)
    window.show_all()

  def on_click(self, widget, window, *args):
    print "On click"
    window.destroy()
    NextWin()

  def mouse_enter_event(self, widget, *args):
    print "Enter"
    widget.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND2))

class NextWin:
  def __init__(self):
    window = gtk.Window()
    vbox = gtk.VBox()
    label = gtk.Label("Change Cursor")
    vbox.pack_start(label, False, False)
    bt = gtk.Button('Ok')
    vbox.pack_start(bt, False, False)
    eventbox = gtk.EventBox()
    window.connect("destroy", lambda w: gtk.main_quit())
    window.set_size_request(400,400)
    window.add(eventbox)
    window.set_name('Next Window')
    window.set_title('Next Window')
    eventbox.add(vbox)
    window.show_all()

gtk.Button = Button
EventBox()
gtk.main()

above code is working for gtk buttons but not working for glade file buttons.以上代码适用于 gtk 按钮,但不适用于林间空地文件按钮。 what could be the problem with glade file buttons?林间空地文件按钮可能有什么问题?

I'm also looking for more appropriate way to change cursor type.我也在寻找更合适的方法来更改 cursor 类型。 Does anyone have any suggestion or correction for above code?有人对上述代码有任何建议或更正吗?

I know this has been sitting here for a while, so just a thought...perhaps rebuild your GUI (or some mockup) outside of Glade.我知道这已经有一段时间了,所以只是一个想法......也许在 Glade 之外重建你的 GUI(或一些模型)。 It really isn't very hard to do, and the documentation is fantastic.这真的不是很难做到,而且文档很棒。

Then, try and hook up your mouse cursor change codes, and see if it works.然后,尝试连接你的鼠标 cursor 更改代码,看看它是否有效。

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

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