PythonでGUIアプリケーション【2】
10月 6th, 2009
カテゴリー:Python勉強
先日書いたウィジェットを別クラスにする『wxPythonでウィジェットを別クラスにする』という記事のコード。色々とやってみて、なんとか動くコードになった。

結局、GUIクラスと実行クラスに分ける方向にしないと今はダメでした。
※この記事はPython2.5.2/WindowsXPという環境で書いています。
まあでも、自分がもっとも望む形になったので良しとしますか。 ひとまずコード。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | # -*- coding: utf-8 -*- import Image import ImageDraw import glob, os import wx class GUI(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(450,100)) self.functions = Events(self) self.panel = wx.Panel(self, -1) self.select = wx.Button (self.panel, -1, "Select", pos=(350,10)) self.text = wx.TextCtrl(self.panel, -1, "Select Folder", pos=(10, 12), size=(330,20)) self.go = wx.Button (self.panel, -1, "Resize", pos=(350,40)) self.Bind(wx.EVT_BUTTON, self.functions.showDlg, id = self.select.GetId()) self.Bind(wx.EVT_BUTTON, self.functions.goResize, id = self.go.GetId()) class Events(): def __init__(self, parent): self.gui = parent def showDlg(self, evt): DirPath = "/Library/Frameworks/Python.framework/Versions/2.4" Dlg = wx.DirDialog(self.gui, message="Please Choose Directory", defaultPath=DirPath) AnsBtn = Dlg.ShowModal() global dirpath dirpath = Dlg.GetPath() if AnsBtn == wx.ID_OK: self.gui.text.SetLabel(dirpath) Dlg.Destroy() def goResize(self, evt): a = 200 size = a, a for infile in glob.glob(os.path.join(dirpath, "*.jpg")): im = Image.open(infile) im.thumbnail(size,Image.ANTIALIAS) box = (((a - im.size[0])/2),((a - im.size[1])/2)) bg = Image.new("RGB",size,"#ffffff") bg.paste(im,box) bg.save(os.path.join(dirpath,os.path.basename(infile))) class Application(wx.App): def __init__(self, redirect = False): wx.App.__init__(self) def OnInit(self): frame = GUI(None, -1, "Resize Image") frame.Show(True) return True Application().MainLoop() |
GUIクラスと実行クラス、そしてアプリケーションの実行クラスという構成。まあこれが一番ベストな形かな。
これから実行クラスを他に作ったり、GUIパーツを作る事もできるようになりました。
前回の記事で分からなかった事を調べるうちにクラスの事など、別の事をたくさん学べました。逆にいい結果になったかも。これからちょくちょく記事に書いていこうと思います。
とりあえず、今日はここまでorz














