开发者

Python GTK menu item takes two clicks to activate

Alright, first off I'm not quite sure how to phrase my problem. This could be lack of sleep, or being pretty new to Python and GTK, or a combination. To aid me, I have written a complete bare-bones example with the help of zetcode.com's tutorials.

The problem, as well as I can put it, is a menu item - with no sub-menus - takes two clicks to activate. Unlike a sub-menu item activating on a single click. This is mildly annoying (and likely to confuse future users), but not really causing any problems with my application. I would, however, like 开发者_运维技巧to resolve it.

My actual application is being created with the help of Ubuntu Quickly - but the problem exists while using gtkBuilder or straight-gtk.

Here is the bare-bones example:

#!/usr/bin/python

import gtk


class MenuTest(gtk.Window):

    def __init__(self):
        super(MenuTest, self).__init__()

        self.set_title("Menus, how do they work?!")
        self.set_size_request(350, 200)
        self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(6400, 6400, 6440))
        self.set_position(gtk.WIN_POS_CENTER)

        mb = gtk.MenuBar()

        filemenu = gtk.Menu()
        filem = gtk.MenuItem("Some Action")
        filem.connect("activate", self.on_file_activate)

        mb.append(filem)

        vbox = gtk.VBox(False, 2)
        vbox.pack_start(mb, False, False, 0)

        self.add(vbox)

        self.connect("destroy", gtk.main_quit)
        self.show_all()

    def on_file_activate(self, widget):
        md = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, "herp derp, took two clicks to show me")
        md.run()
        md.destroy()


MenuTest()
gtk.main()

Hopefully someone can help, and not completely confuse this noob at the same time.


You can solve your problem by connecting to the 'button-press-event' signal instead of the 'activate' signal, and making your callback like this:

def on_file_activate(self, widget, event):
    if event.button != 1:
        return False  #only intercept left mouse button
    md = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, "herp derp, I only needed one click")
    md.run()
    md.destroy()
    return True

However, why would you want to do that? I'm not surprised that your original code didn't work as expected, because that's not really what menus are for. You'd be better off using a toolbar button, or a regular button. I think misusing a menu as a button is more likely to confuse future users.


I know this is a fairly old thread. But, for the sake of anyone else trying to accomplish this task, the simplest solution is to replace the "activate" signal with the "select" signal. That should fix it. At least, it does on my box.

ie. replace

filem.connect("activate", self.on_file_activate)

with

filem.connect("select", self.on_file_activate)

I would also change the function name for the sake of clarity.

I hope that helps someone. =)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜