Python的GUI庫很多,在這裡選用的是wxPython,安裝後在Windows下就有以下的工具跟教學範例,其中XRC Resource Editor就是要用來設計GUI的圖型化工具。
打開後如以下一般。設計好之後可以存成XRC檔或直接生成 .py 檔案,現在要使用XRC檔來做連結,將來也方便修改語系問題。
resource1.xrc檔
1: <?xml version="1.0" ?>
2: <resource>
3: <object class="wxFrame" name="FRAME1">
4: <title>測試窗口</title>
5: <icon>favicon.ico</icon>
6: <object class="wxPanel" name="MyPanel">
7: <object class="wxButton" name="CloseButton">
8: <label>關閉</label>
9: <pos>15,10</pos>
10: </object>
11: </object>
12: </object>
13: </resource>
import wx import wx.xrc as xrc class MyApp(wx.App): def OnInit(self): self.res = xrc.XmlResource(r"./resource1.xrc") self.frame_1 = self.res.LoadFrame(None, "FRAME1") self.Bind(wx.EVT_BUTTON, self.OnClose, id=xrc.XRCID('CloseButton')) self.frame_1.Show() return 1 # End of OnInit(). def OnClose(self, event): self.frame_1.Close() # End of class MyApp. if __name__ == "__main__": app = MyApp(0) app.MainLoop()執行結果
沒有留言:
張貼留言