{"id":1021,"date":"2012-05-13T18:51:21","date_gmt":"2012-05-13T09:51:21","guid":{"rendered":"http:\/\/helloidea.org\/?p=1021"},"modified":"2014-02-22T22:17:54","modified_gmt":"2014-02-22T13:17:54","slug":"wxpython%e3%83%a1%e3%83%a2","status":"publish","type":"post","link":"https:\/\/helloidea.org\/index.php\/archives\/1021.html","title":{"rendered":"wxPython\u30e1\u30e2"},"content":{"rendered":"<p>wxPython\u3002\u5206\u304b\u308a\u3084\u3059\u304f\u30e1\u30e2\u30e1\u30e2<br \/>\n\u7c21\u5358\u306bGUI\u304c\u3064\u304f\u308c\u3066\u826f\u3044\u611f\u3058\u3002<\/p>\n<p>\u30c6\u30ad\u30b9\u30c8\u30922\u3064\u8aad\u307f\u8fbc\u3093\u3067\u7d44\u307f\u5408\u308f\u305b\u3092\u8868\u793a\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u3067\u3059<br \/>\n<a href=\"http:\/\/helloidea.org\/wp-content\/uploads\/2012\/05\/TestApp1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/helloidea.org\/wp-content\/uploads\/2012\/05\/TestApp1-300x133.png\" alt=\"\" title=\"TestApp\" width=\"300\" height=\"133\" class=\"alignnone size-medium wp-image-1046\" srcset=\"https:\/\/helloidea.org\/wp-content\/uploads\/2012\/05\/TestApp1-300x133.png 300w, https:\/\/helloidea.org\/wp-content\/uploads\/2012\/05\/TestApp1.png 900w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#! C:\\Python27\\python.exe\r\n# -*- coding: shift-jis -*-\r\n\r\nimport wx       # wxPython\r\nimport xlwt     # Excel(\u66f8\u304d\u8fbc\u307f)\r\nimport sys      # \u3044\u308d\u3044\u308d\r\n\r\n#-----------------------------------------------------------------------------\r\n#    \u57fa\u672c\u30d5\u30ec\u30fc\u30e0 \u57fa\u672c\u30d1\u30cd\u30eb\r\n#-----------------------------------------------------------------------------\r\nclass TestAppFrame(wx.Frame):\r\n\r\n    Appname = (&quot;TestApp&quot;)\r\n\r\n    # \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u7684\u306a\u3082\u306e self\u306f\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\r\n    def __init__(self):\r\n        wx.Frame.__init__(self, None, wx.ID_ANY, self.Appname, size=(900,400))\r\n\r\n        # \u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f in1.txt\r\n        try:\r\n            f = open(&quot;in1.txt&quot;, &quot;r&quot;)\r\n            load_str1 = f.read()\r\n\r\n            f.close()\r\n        except IOError:\r\n            self.showMessage(&quot;in1.txt\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093&quot;)\r\n            sys.exit()\r\n\r\n        # \u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f in2.txt\r\n        try:\r\n            f = open(&quot;in2.txt&quot;, &quot;r&quot;)\r\n            load_str2 = f.read()\r\n\r\n            f.close()\r\n        except IOError:\r\n            self.showMessage(&quot;in2.txt\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093&quot;)\r\n            sys.exit()\r\n\r\n        # shift-jis \u3092 unicode\u578b\u306b\u30c7\u30b3\u30fc\u30c9 (\u5f8c\u3005\u6587\u5b57\u30b3\u30fc\u30c9\u3067\u30cf\u30de\u308a\u3084\u3059\u3044\u306e\u3067\u5185\u90e8\u3067\u306fUnicode\u578b\u306b\u7d71\u4e00\u3059\u308b\u3002\u3089\u3057\u3044)\r\n        load_str1.decode(&#039;shift-jis&#039;)\r\n        load_str2.decode(&#039;shift-jis&#039;)\r\n\r\n        # \u8981\u7d20\u4e00\u89a7\u306e\u751f\u6210\r\n        load_list1 = load_str1.split(&quot;\\n&quot;)\r\n        load_list2 = load_str2.split(&quot;\\n&quot;)\r\n\r\n        # \u30e1\u30a4\u30f3\u30d1\u30cd\u30eb\u751f\u6210 (1\u3064)\r\n        main_panel = wx.Panel(self, wx.ID_ANY)\r\n\r\n        # \u30b5\u30d6\u30d1\u30cd\u30eb\u751f\u6210 (2\u3064)\r\n        sub_list_panel = list_panel(main_panel, load_list1, load_list2) # \u30ea\u30b9\u30c8\u30d1\u30cd\u30eb\r\n        sub_button_panel = button_panel(main_panel)                     # \u30dc\u30bf\u30f3\u30d1\u30cd\u30eb\r\n\r\n        # \u30e1\u30a4\u30f3\u30ec\u30a4\u30a2\u30a6\u30c8 (\u30e1\u30a4\u30f3\u30d1\u30cd\u30eb\u306e\u4e0a\u306b\u30b5\u30d6\u30d1\u30cd\u30eb\u3092\u914d\u7f6e\u3059\u308b)\r\n        main_layout = wx.FlexGridSizer(1,2)\r\n        main_layout.Add(sub_list_panel, flag=wx.GROW)\r\n        main_layout.Add(sub_button_panel, flag=wx.GROW)\r\n        main_layout.AddGrowableCol(0)\r\n        main_layout.AddGrowableRow(0)\r\n\r\n        # \u30ec\u30a4\u30a2\u30a6\u30c8\u78ba\u5b9a\r\n        main_panel.SetSizer(main_layout)\r\n\r\n    def showMessage(self, word):\r\n        dig = wx.MessageDialog(self, word, &quot;\u30a8\u30e9\u30fc&quot;, wx.OK)\r\n        dig.ShowModal()\r\n        dig.Destroy()\r\n\r\n#-----------------------------------------------------------------------------\r\n#    \u30ea\u30b9\u30c8\u30d1\u30cd\u30eb\r\n#-----------------------------------------------------------------------------\r\nclass list_panel(wx.Panel):\r\n\r\n    # public\u306a\u611f\u3058\r\n    data = &#x5B;]\r\n\r\n    def __init__(self, main_panel, listdata1, listdata2):\r\n        wx.Panel.__init__(self, main_panel, wx.ID_ANY)\r\n\r\n        # self.XXX\u306f private\u306a\u611f\u3058\r\n        self.stage1 = listdata1\r\n        self.stage2 = listdata2\r\n        self.stage3 = &#x5B;]\r\n\r\n        # \u30ea\u30b9\u30c8\u30dc\u30c3\u30af\u30b9\u306e\u751f\u6210 (3\u3064)\r\n        self.listbox1 = wx.ListBox(self, wx.ID_ANY, size=(200,-1), choices=self.stage1, style=wx.LB_HSCROLL|wx.LB_MULTIPLE)\r\n        self.listbox2 = wx.ListBox(self, wx.ID_ANY, size=(200,-1), choices=self.stage2, style=wx.LB_HSCROLL|wx.LB_MULTIPLE)\r\n        self.listbox3 = wx.ListBox(self, wx.ID_ANY, size=(300,-1), choices=self.stage3, style=wx.LB_HSCROLL|wx.LB_SINGLE)\r\n\r\n        # \u30ea\u30b9\u30c8\u30dc\u30c3\u30af\u30b9\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u767b\u9332\r\n        self.listbox1.Bind(wx.EVT_LISTBOX, self.stage1_select)\r\n        self.listbox2.Bind(wx.EVT_LISTBOX, self.stage2_select)\r\n\r\n        # \u30ea\u30b9\u30c8\u30d1\u30cd\u30eb\u30ec\u30a4\u30a2\u30a6\u30c8(\u30ea\u30b9\u30c8\u30d1\u30cd\u30eb\u306e\u4e0a\u306b\u30ea\u30b9\u30c8\u30a6\u30a3\u30b8\u30a7\u30c3\u30c8\u3092\u914d\u7f6e\u3059\u308b)\r\n        layout = wx.FlexGridSizer(1,3)\r\n        layout.Add(self.listbox1, flag=wx.GROW|wx.TOP|wx.LEFT|wx.BOTTOM, border=10)\r\n        layout.Add(self.listbox2, flag=wx.GROW|wx.TOP|wx.LEFT|wx.BOTTOM, border=10)\r\n        layout.Add(self.listbox3, flag=wx.GROW|wx.TOP|wx.LEFT|wx.BOTTOM, border=10)\r\n        layout.AddGrowableCol(0)\r\n        layout.AddGrowableCol(1)\r\n        layout.AddGrowableCol(2)\r\n        layout.AddGrowableRow(0)\r\n\r\n        # \u30ec\u30a4\u30a2\u30a6\u30c8\u78ba\u5b9a\r\n        self.SetSizer(layout)\r\n\r\n    def refresh(self):\r\n        self.cache1 = list(self.listbox1.GetSelections())\r\n        self.cache2 = list(self.listbox2.GetSelections())\r\n\r\n        self.data = &#x5B;]\r\n        for i in range(len(self.cache2)):\r\n            for ii in range(len(self.cache1)):\r\n                self.data.append( self.stage2&#x5B;self.cache2&#x5B;i]] + &quot; x &quot; + self.stage1&#x5B;self.cache1&#x5B;ii]] )\r\n\r\n        self.listbox3.Set(self.data)\r\n\r\n    def stage1_select(self, event):\r\n        self.refresh()\r\n\r\n    def stage2_select(self, event):\r\n        self.refresh()\r\n\r\n#-----------------------------------------------------------------------------\r\n#    \u30dc\u30bf\u30f3\u30d1\u30cd\u30eb\r\n#-----------------------------------------------------------------------------\r\nclass button_panel(wx.Panel):\r\n    def __init__(self, main_panel):\r\n        wx.Panel.__init__(self, main_panel, wx.ID_ANY)\r\n\r\n        # \u30dc\u30bf\u30f3\u306e\u751f\u6210 (2\u3064)\r\n        button1 = wx.Button(self, wx.ID_ANY, &quot;\u5168\u3066\u30c6\u30ad\u30b9\u30c8\u3067\u51fa\u529b&quot;)\r\n        button2 = wx.Button(self, wx.ID_ANY, &quot;\u5168\u3066Excel\u3067\u51fa\u529b&quot;)\r\n\r\n        # \u30dc\u30bf\u30f3\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u767b\u9332\r\n        button1.Bind(wx.EVT_BUTTON, self.click_button_1)\r\n        button2.Bind(wx.EVT_BUTTON, self.click_button_2)\r\n\r\n        # \u30dc\u30bf\u30f3\u30d1\u30cd\u30eb\u30ec\u30a4\u30a2\u30a6\u30c8(\u30dc\u30bf\u30f3\u30d1\u30cd\u30eb\u306e\u4e0a\u306b\u30dc\u30bf\u30f3\u30a6\u30a3\u30b8\u30a7\u30c3\u30c8\u3092\u914d\u7f6e\u3059\u308b)\r\n        layout = wx.BoxSizer(wx.VERTICAL)\r\n        layout.Add(button1, flag=wx.TOP|wx.LEFT|wx.RIGHT, border=10)\r\n        layout.Add(button2, flag=wx.TOP|wx.LEFT|wx.RIGHT, border=10)\r\n\r\n        # \u30ec\u30a4\u30a2\u30a6\u30c8\u78ba\u5b9a\r\n        self.SetSizer(layout)\r\n\r\n    def click_button_1(self, event):\r\n        f = open(&quot;out.txt&quot;, &quot;w&quot;)\r\n        for word in list_panel.data:    # \u3053\u3093\u306a\u611f\u3058\u306b\u4ed6\u306e\u30af\u30e9\u30b9\u306e\u5909\u6570\u306b\u30a2\u30af\u30bb\u30b9\u53ef\u80fd\r\n            f.write(word.encode(&#039;shift-jis&#039;) + &quot;\\n&quot;)    # \u4f55\u3089\u304b\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u306b\u30a8\u30f3\u30b3\u30fc\u30c9\u3057\u3066\u304b\u3089write\r\n        f.close()\r\n\r\n        self.showMessage(&quot;out.txt \u3092\u51fa\u529b\u3057\u307e\u3057\u305f&quot;)\r\n\r\n    def click_button_2(self, event):\r\n\r\n        book = xlwt.Workbook()\r\n        sheet = book.add_sheet(&quot;HAHAHA!!&quot;)\r\n        sheet.write(1,1,&quot;ALL list&quot;)\r\n\r\n        for i, word in enumerate(list_panel.data):\r\n            sheet.write(i + 2,1,word)                   # Unicode\u5f62\u5f0f\u306e\u307e\u307ewrite\r\n\r\n        book.save(&quot;out.xls&quot;)\r\n\r\n        self.showMessage(&quot;out.xls \u3092\u51fa\u529b\u3057\u307e\u3057\u305f&quot;)\r\n\r\n    def showMessage(self, word):\r\n        dig = wx.MessageDialog(self, word, &quot;out&quot;, wx.OK)\r\n        dig.ShowModal()\r\n        dig.Destroy()\r\n\r\n#-----------------------------------------------------------------------------\r\n#    main \u3053\u3053\u304b\u3089\u306f\u3058\u307e\u308b\r\n#-----------------------------------------------------------------------------\r\nif __name__ == &quot;__main__&quot;: # main() \u306a\u611f\u3058\r\n\r\n    app = wx.App()\r\n    frame = TestAppFrame()      # \u30af\u30e9\u30b9\u304c\u751f\u6210\u3055\u308c\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u7684\u306b__init()__\u304c\u547c\u3073\u51fa\u3055\u308c\u308b\r\n\r\n    frame.Show()\r\n    app.MainLoop()\r\n<\/pre>\n<p><\u53c2\u8003\u30b5\u30a4\u30c8><br \/>\n<a href=\"http:\/\/www.python-izm.com\/\" target=\"_blank\">python-izm<\/a><br \/>\n<a href=\"http:\/\/d.hatena.ne.jp\/ymotongpoo\/20081206\/1228542329\" target=\"_blank\">YAMAGUCHI::weblog &#8211; \u30de\u30eb\u30c1\u30d0\u30a4\u30c8\u6587\u5b57\u5217\u306e\u30a8\u30f3\u30b3\u30fc\u30c9\u30fb\u30c7\u30b3\u30fc\u30c9<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>wxPython\u3002\u5206\u304b\u308a\u3084\u3059\u304f\u30e1\u30e2\u30e1\u30e2<br \/>\n\u7c21\u5358\u306bGUI\u304c\u3064\u304f\u308c\u3066\u826f\u3044\u611f\u3058\u3002<br \/>\n\u30c6\u30ad\u30b9\u30c8\u30922\u3064\u8aad\u307f\u8fbc\u3093\u3067\u7d44\u307f\u5408\u308f\u305b\u3092\u8868\u793a\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u3067\u3059<\/p>\n","protected":false},"author":1,"featured_media":1046,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[183,184],"class_list":["post-1021","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-memo","tag-python","tag-wxpython"],"_links":{"self":[{"href":"https:\/\/helloidea.org\/index.php\/wp-json\/wp\/v2\/posts\/1021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/helloidea.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/helloidea.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/helloidea.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/helloidea.org\/index.php\/wp-json\/wp\/v2\/comments?post=1021"}],"version-history":[{"count":27,"href":"https:\/\/helloidea.org\/index.php\/wp-json\/wp\/v2\/posts\/1021\/revisions"}],"predecessor-version":[{"id":1250,"href":"https:\/\/helloidea.org\/index.php\/wp-json\/wp\/v2\/posts\/1021\/revisions\/1250"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/helloidea.org\/index.php\/wp-json\/wp\/v2\/media\/1046"}],"wp:attachment":[{"href":"https:\/\/helloidea.org\/index.php\/wp-json\/wp\/v2\/media?parent=1021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/helloidea.org\/index.php\/wp-json\/wp\/v2\/categories?post=1021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/helloidea.org\/index.php\/wp-json\/wp\/v2\/tags?post=1021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}