PyGTK如何在弹出窗口中单击按钮并打开文件

时间:2011-05-06 16:58:50

标签: python menu pygtk

我编辑了代码,以便现在显示3个按钮。有人请告诉我如何制作它,以便当我点击Helloworld按钮并在另一个窗口中弹出名为Helloworld.py的应用程序时。对于其他2个按钮

!/usr/bin/env python

#  menu.py

import pygtk
pygtk.require('2.0')
import gtk

class MenuExample:
    def __init__(self):
        # create a new window
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_size_request(200, 100)
        window.set_title("GTK Menu Test")
        window.connect("delete_event", lambda w,e: gtk.main_quit())

        # Init the menu-widget, and remember -- never
        # show() the menu widget!! 
        # This is the menu that holds the menu items, the one that
        # will pop up when you click on the "Root Menu" in the app
        menu = gtk.Menu()

        # Next we make a little loop that makes three menu-entries for
        # "test-menu".  Notice the call to gtk_menu_append.  Here we are
        # adding a list of menu items to our menu.  Normally, we'd also
        # catch the "clicked" signal on each of the menu items and setup a
        # callback for it, but it's omitted here to save space.
        for i in range(3):
            # Copy the names to the buf.
            buf = "Test-undermenu - %d" % i

            # Create a new menu-item with a name...
            menu_items = gtk.MenuItem(buf)

            # ...and add it to the menu.
            menu.append(menu_items)

        # Do something interesting when the menuitem is selected
        menu_items.connect("activate", self.menuitem_response, buf)

            # Show the widget
            menu_items.show()

        # This is the root menu, and will be the label
        # displayed on the menu bar.  There won't be a signal handler attached,
        # as it only pops up the rest of the menu when pressed.
        root_menu = gtk.MenuItem("Root Menu")

        root_menu.show()

        # Now we specify that we want our newly created "menu" to be the
        # menu for the "root menu"
        root_menu.set_submenu(menu)

        # A vbox to put a menu and a button in:
        vbox = gtk.VBox(False, 0)
        window.add(vbox)
        vbox.show()

        # Create a menu-bar to hold the menus and add it to our main window
        menu_bar = gtk.MenuBar()
        vbox.pack_start(menu_bar, False, False, 2)
        menu_bar.show()

        # Create a button to which to attach menu as a popup
        button = gtk.Button("HelloWorld")
        button.connect_object("event", self.button_press, menu)
        vbox.pack_end(button, True, True, 2)
        button.show()

        button2 = gtk.Button("Scrible")
        button2.connect_object("event", self.button_press, menu)
        vbox.pack_end(button2, True, True, 2)
        button2.show()

        button3 = gtk.Button("Final")
        button3.connect_object("event", self.button_press, menu)
        vbox.pack_end(button3, True, True, 2)
        button3.show()

        # And finally we append the menu-item to the menu-bar -- this is the
        # "root" menu-item I have been raving about =)
        menu_bar.append (root_menu)

        # always display the window as the last step so it all splashes on
        # the screen at once.
        window.show()

    # Respond to a button-press by posting a menu passed in as widget.
    #
    # Note that the "widget" argument is the menu being posted, NOT
    # the button that was pressed.
    def button_press(self, widget, event):
        if event.type == gtk.gdk.BUTTON_PRESS:
            widget.popup(None, None, None, event.button, event.time)
            # Tell calling code that we have handled this event the buck
            # stops here.
            return True
        # Tell calling code that we have not handled this event pass it on.
        return False

    def button2_press(self, widget, event):
        if event.type == gtk.gdk.BUTTON2_PRESS:
            widget.popup(None, None, None, event.button, event.time)
            return True
        return False

    def button3_press(self, widget, event):
        if event.type == gtk.gdk.BUTTON3_PRESS:
            widget.popup(None, None, None, event.button, event.time)
            return True
        return False

    # Print a string when a menu item is selected
    def menuitem_response(self, widget, string):
        print "%s" % string

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    MenuExample()
    main()

1 个答案:

答案 0 :(得分:2)

你可以这样做。我假设您只想执行.py文件,例如helloworld.py等我正在使用来自 subprocess Popen 来执行python(不假设py文件是可执行的)脚本。请注意,我已将脚本编辑为只有一个按钮,这只是为了向您展示这个想法。

import pygtk
pygtk.require('2.0')
import gtk

import subprocess

class Example:
    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_size_request(200, 100)
        window.set_title("GTK Menu Test")
        window.connect("delete_event", 
                       lambda w,e: gtk.main_quit())

        vbox = gtk.VBox(False, 0)
        window.add(vbox)
        vbox.show()

        button = gtk.Button("HelloWorld")
        button.connect("clicked", self.clicked_helloworld)
        vbox.pack_end(button, True, True, 2)
        button.show()

        window.show_all()

    def clicked_helloworld(self, widget):
        subprocess.Popen(["python", "helloworld.py"])

    def main(self):
        gtk.main()
        return 0

Example().main()