2013년 12월 17일 화요일

[Python] 파이썬 노트패드 예제

자료출처: 마이크로소프트웨어 2002년 4월호

파이썬/GTK+로 간단한 응용 프로그램 만들기(1)

#!/usr/bin/env python

from gtk import *
import GtkExtra

class Notepad(GtkWindow):
        def __init__(self):
                GtkWindow.__init__(self, WINDOW_TOPLEVEL)
                self.set_default_size(475, 325)

                self.menuHandleBox = GtkHandleBox()
                self.notepadMenufactory = self.buildMenu()
                self.menuHandleBox.add(self.notepadMenufactory)
                self.textArea = GtkText()
                self.textArea.set_editable(TRUE)
                self.resetTextAreaChanged()
                self.fileName = "Untitled"
                self.set_title("Untitled")

                vbox = GtkVBox()
                vbox.pack_start(self.menuHandleBox, expand = FALSE)
                vbox.pack_start(self.textArea, padding = 2)

                self.add(vbox)
               
                # connecting signals and the corresponding callback functions
                self.connect("destroy", self.quit)
                self.textArea.connect("changed", self.textAreaChanged)
       
                self.show_all()


        def buildMenu(self):
                menufactory = GtkExtra.MenuFactory()
                menufactory.add_entries([
                        ('File/New', '<control>N', self.file_new),
                        ('File/Open...', '<control>O', self.file_open),
                        ('File/Save...', '<control>S', self.file_save),
                        ('File/Save As...', '<control>V', self.file_save_as),
                        ('File/<separator>', None, None),
                        ('File/Exit', '<control>Q', self.file_exit),
                        ('Help/About...', '<control>A', self.help_about)
                ])
                self.add_accel_group(menufactory.accelerator)
                return menufactory

        def getTextAreaChanged(self):
                return self.isTextAreaChanged
        def resetTextAreaChanged(self):
                self.isTextAreaChanged = FALSE
               
        def setFileName(self, fileName):
                self.fileName = fileName
        def getFileName(self):
                return self.fileName
        def resetFileName(self):
                self.fileName = "Untitled"

        def clearTextArea(self):
                textLength = self.textArea.get_length()
                self.textArea.set_point(textLength)
                self.textArea.backward_delete(textLength)

        def set_title(self, title):
                GtkWindow.set_title(self, title)
                self.title = title
        def get_title(self):
                return self.title

        # callback functions
        def quit(self, *args):
                self.hide()
                mainquit()

        def textAreaChanged(self, *args):
                if self.isTextAreaChanged != TRUE:
                        self.isTextAreaChanged = TRUE
                        self.set_title(self.get_title() + '*')

        def file_new(self, *args):
                if self.getTextAreaChanged() == TRUE:        # if the user typed in something
                        choice = GtkExtra.message_box(title = "Warning!",
                                        message = "WARNING: Your current work will be destroyed. "
                                        + "Are you sure?",
                                        buttons = ("Yes", "No"))
                        if choice == "No":
                                return                        # do nothing
                self.clearTextArea()
                self.resetFileName()
                self.resetTextAreaChanged()
                self.set_title(self.getFileName())

        def file_open(self, *args):
                if self.getTextAreaChanged() == TRUE:
                        choice = GtkExtra.message_box(title = "Warning!",
                                        message = "Opening an existing file will delete current"
                                        + " one. Are you sure?",
                                        buttons = ("Yes", "No"))
                        if choice == "No":
                                return
                fileName = GtkExtra.file_open_box()
                if fileName == None: return                        # No file selected
                self.setFileName(fileName)
               
                try:
                        input = open(fileName, 'r')
                        text = input.read()
                        input.close()
                except:
                        GtkExtra.message_box(title = "Error",
                                        message = "Error opening a file!",
                                        buttons = ("OK",))
                        return
                self.clearTextArea()
                self.textArea.insert_defaults(text)
                self.resetTextAreaChanged()
                self.set_title(fileName)
               
        def file_save(self, *args):
                fileName = self.getFileName()
                if fileName == "Untitled":
                        self.file_save_as()
                else:
                        self._file_save_name(fileName)

        def file_save_as(self, *args):
                self._file_save_name(GtkExtra.file_save_box())

        def _file_save_name(self, fileName):
                if fileName == None:
                        return                                        # No file selected by the user
                try:
                        output = open(fileName, 'w')
                        text = self.textArea.get_chars(0, self.textArea.get_length())
                        output.write(text)
                        output.close()
                except:
                        GtkExtra.message_box(title = "Error",
                                        message = "Error writing a file!",
                                        buttons = ("OK",))
                        return
                self.setFileName(fileName)
                self.resetTextAreaChanged()
                self.set_title(fileName)
               
        def file_exit(self, *args):
                choice = GtkExtra.message_box(title = "Quit",
                                        message = "Do you want to quit the application now?",
                                        buttons = ("Yes", "No"))
                if choice == "No":
                        return
                self.quit()

        def help_about(self, mi=None):
                GtkExtra.message_box(title = "About...",
                                message = "My simple GTK notepad :)",
                                buttons = ("OK",),
                                pixmap = "notepad.xpm")

if __name__ == '__main__':
        app = Notepad()
        mainloop()

댓글 없음:

댓글 쓰기