如何将众多程序合并为一个代码?

时间:2011-05-09 14:25:32

标签: python pygtk connect combinations modular

我有一个程序可以打开许多其他程序。

如何将其他程序中的代码组合到此代码中,以便代替10组代码,我只会有一组代码?

我希望将所有代码放入的应用代码:

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(600, 600)
        window.set_title("GTK Menu")
        window.connect("delete_event",
                       lambda w,e: gtk.main_quit())

    # Add Vbox so that we can have numerous widgets
        vbox = gtk.VBox(False, 0)
        window.add(vbox)
        vbox.show()

        #Adding a status bar
        self.statusbar = gtk.Statusbar()
        vbox.pack_start(self.statusbar, False, False, 0)


    #Adding 5 buttons one for each of the activities
        button = gtk.Button("Write")
        button.connect("clicked", self.clicked_Write)
        vbox.pack_end(button, True, True, 2)
        button.show()

        button2 = gtk.Button("Draw")
        button2.connect("clicked", self.clicked_Scrible)
        vbox.pack_end(button2, True, True, 2)
        button2.show()

        button3 = gtk.Button("Final Test")
        button3.connect("clicked", self.clicked_Final)
        vbox.pack_end(button3, True, True, 2)
        button3.show()

        button4 = gtk.Button("Helloworld")
        button4.connect("clicked", self.clicked_Hello)
        vbox.pack_end(button4, True, True, 2)
        button4.show()

        button5 = gtk.Button("Facebook")
        button5.connect("clicked", self.clicked_Facebook)
        vbox.pack_end(button5, True, True, 2)
        button5.show()

        button6 = gtk.Button("SpinButtons")
        button6.connect("clicked", self.clicked_Spin)
        vbox.pack_end(button6, True, True, 2)
        button6.show()

        button7 = gtk.Button("Calendar")
        button7.connect("clicked", self.clicked_Cal)
        vbox.pack_end(button7, True, True, 2)
        button7.show()

        button8 = gtk.Button("Colour Wheel(click popup)")
        button8.connect("clicked", self.clicked_Wheel)
        vbox.pack_end(button8, True, True, 2)
        button8.show()

        button9 = gtk.Button("Choose File")
        button9.connect("clicked", self.clicked_File)
        vbox.pack_end(button9, True, True, 2)
        button9.show()

        button10 = gtk.Button("Word Completer")
        button10.connect("clicked", self.clicked_Word)
        vbox.pack_end(button10, True, True, 2)
        button10.show()

        window.show_all()




    def clicked_Write(self, widget):
        # push a new message to the statusbar, using context_id 0
        self.statusbar.push(0, "You have oppened Write")
        subprocess.Popen(["python", "Helloworld.py"])

    def clicked_Scrible(self, widget):
        self.statusbar.push(0, "You have opened the Drawind Pad")
        subprocess.Popen(["python", "scrible.py"])

    def clicked_Final(self, widget):
        self.statusbar.push(0, "You have opened the Final Exam")
        subprocess.Popen(["python", "Final.py"])

    def clicked_Hello(self, widget):
        self.statusbar.push(0, "You have opened Helloworld")
        subprocess.Popen(["python", "Helloword.py"])

    def clicked_Facebook(self, widget):
        self.statusbar.push(0, "You have opened Facebook")
        subprocess.Popen(["python", "facebookfinal.py"])

    def clicked_Spin(self, widget):
        self.statusbar.push(0, "You have opened the Spin Buttons App")
        subprocess.Popen(["python", "SpinButtons.py"])

    def clicked_Cal(self, widget):
        self.statusbar.push(0, "You have opened the Calender")
        subprocess.Popen(["python", "Calender.py"])

    def clicked_Wheel(self, widget):
        self.statusbar.push(0, "You have opened the Colour Wheel")
        subprocess.Popen(["python", "colour.py"])

    def clicked_File(self, widget):
        self.statusbar.push(0, "You have opened File Chooser")
        subprocess.Popen(["python", "filechooser.py"])

    def clicked_Word(self, widget):
        self.statusbar.push(0, "You have opened the Word Completer")
        subprocess.Popen(["python", "finisher.py"])



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

Example().main()

以下是我打开的其中一个程序的代码

Colour.py

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

class ColorSelectionExample:
    # Color changed handler
    def color_changed_cb(self, widget):
        # Get drawingarea colormap
        colormap = self.drawingarea.get_colormap()

        # Get current color
        color = self.colorseldlg.colorsel.get_current_color()

        # Set window background color
        self.drawingarea.modify_bg(gtk.STATE_NORMAL, color)

    # Drawingarea event handler
    def area_event(self, widget, event):
        handled = False

        # Check if we've received a button pressed event
        if event.type == gtk.gdk.BUTTON_PRESS:
            handled = True

            # Create color selection dialog
            if self.colorseldlg == None:
                self.colorseldlg = gtk.ColorSelectionDialog(
                    "Select background color")

            # Get the ColorSelection widget
            colorsel = self.colorseldlg.colorsel

            colorsel.set_previous_color(self.color)
            colorsel.set_current_color(self.color)
            colorsel.set_has_palette(True)

            # Connect to the "color_changed" signal
            colorsel.connect("color_changed", self.color_changed_cb)
            # Show the dialog
            response = self.colorseldlg.run()

            if response -- gtk.RESPONSE_OK:
                self.color = colorsel.get_current_color()
            else:
                self.drawingarea.modify_bg(gtk.STATE_NORMAL, self.color)

            self.colorseldlg.hide()

        return handled

    # Close down and exit handler
    def destroy_window(self, widget, event):
        gtk.main_quit()
        return True

    def __init__(self):
        self.colorseldlg = None
        # Create toplevel window, set title and policies
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Color selection test")
        window.set_resizable(True)

        # Attach to the "delete" and "destroy" events so we can exit
        window.connect("delete_event", self.destroy_window)

        # Create drawingarea, set size and catch button events
        self.drawingarea = gtk.DrawingArea()

        self.color = self.drawingarea.get_colormap().alloc_color(0, 65535, 0)

        self.drawingarea.set_size_request(200, 200)
        self.drawingarea.set_events(gtk.gdk.BUTTON_PRESS_MASK)
        self.drawingarea.connect("event",  self.area_event)

        # Add drawingarea to window, then show them both
        window.add(self.drawingarea)
        self.drawingarea.show()
        window.show()

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

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

1 个答案:

答案 0 :(得分:0)

将文件放在同一目录中,然后使用以下内容:

from Colour import ColorSelectionExample

然后运行它,构造一个ColorSelectionExample:

ColorSelectionExample()
相关问题