Python/wxPython/QuickStart: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 47: Line 47:
</source>
</source>
|}
|}
== wxFrame with AUI ==
<source lang="python">
#!/usr/bin/env python3
import wx
import wx.aui
import wx.lib.wxcairo
class MyWindow(wx.Window):
    def __init__(self, parent):
        super().__init__(parent, wx.ID_ANY, size=(200, 100))
        self.Bind(wx.EVT_PAINT, self.OnPaint)
    def OnPaint(self, evt):
        dc = wx.ClientDC(self)
        dc.DrawRectangle(0, 0, 200, 200)
class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(None, wx.ID_ANY, 'Aui Sandbox', size=(1000, 650))
        text1 = wx.TextCtrl(self)
        text2 = wx.TextCtrl(self)
        mywin1 = MyWindow(self)
        self.mgr = wx.aui.AuiManager(self)
        self.mgr.AddPane(text1, wx.LEFT, 'Pane 1')
        self.mgr.AddPane(text2, wx.BOTTOM, 'Pane 2')
        self.mgr.AddPane(mywin1, wx.RIGHT, 'Pane 3')
        self.mgr.Update()
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Centre()
        self.Show()
    def OnClose(self, evt):
        self.mgr.UnInit()
        self.Destroy()
def main():
    app = wx.App()
    MyFrame()
    app.MainLoop()
if __name__ == '__main__':
    main()
</source>


== wxMDIParentFrame without XRC ==
== wxMDIParentFrame without XRC ==
Line 62: Line 112:
     app = MyApp()
     app = MyApp()
     app.MainLoop()
     app.MainLoop()
</python>
</source>


== wxMDIParentFrame with XRC ==
== wxMDIParentFrame with XRC ==
Line 69: Line 119:
! Python (main.py) || (layout.py)
! Python (main.py) || (layout.py)
|-
|-
|  
| valign="top" |
<source lang="python">
<source lang="python">
#!/usr/bin/env python3
#!/usr/bin/env python3
Line 85: Line 135:
     app.MainLoop()
     app.MainLoop()
</source>
</source>
|
| valign="top" |
<source lang="xml">
<source lang="xml">
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>

Latest revision as of 08:19, 22 March 2019

Skeleton

wxFrame without XRC

#!/usr/bin/env python3
import wx

class MyApp(wx.App):
    def OnInit(self):
        frame = wx.Frame(None, -1, 'Hello World!')
        frame.Show()
        return True

if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()

wxFrame with XRC

Python (main.py) XRC (layout.xrc)
#!/usr/bin/env python3
import wx.xrc

class MyApp(wx.App):
    def OnInit(self):
        res = wx.xrc.XmlResource('layout.xrc')
        frame = res.LoadFrame(None, 'mainFrame')
        frame.Show()
        return True

if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()
<?xml version="1.0" encoding="utf-8"?>
<resource version="2.9.0.5">
  <object name="mainFrame" class="wxFrame">
    <title>Hello World!</title>
  </object>
</resource>

wxFrame with AUI

#!/usr/bin/env python3

import wx
import wx.aui
import wx.lib.wxcairo

class MyWindow(wx.Window):

    def __init__(self, parent):
        super().__init__(parent, wx.ID_ANY, size=(200, 100))
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self, evt):
        dc = wx.ClientDC(self)
        dc.DrawRectangle(0, 0, 200, 200)

class MyFrame(wx.Frame):

    def __init__(self):
        super().__init__(None, wx.ID_ANY, 'Aui Sandbox', size=(1000, 650))

        text1 = wx.TextCtrl(self)
        text2 = wx.TextCtrl(self)
        mywin1 = MyWindow(self)

        self.mgr = wx.aui.AuiManager(self)
        self.mgr.AddPane(text1, wx.LEFT, 'Pane 1')
        self.mgr.AddPane(text2, wx.BOTTOM, 'Pane 2')
        self.mgr.AddPane(mywin1, wx.RIGHT, 'Pane 3')
        self.mgr.Update()

        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Centre()
        self.Show()

    def OnClose(self, evt):
        self.mgr.UnInit()
        self.Destroy()

def main():
    app = wx.App()
    MyFrame()
    app.MainLoop()

if __name__ == '__main__':
    main()

wxMDIParentFrame without XRC

#!/usr/bin/env python3
import wx

class MyApp(wx.App):
    def OnInit(self):
        frame = wx.MDIParentFrame(None, -1, 'Hello World!')
        frame.Show()
        return True

if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()

wxMDIParentFrame with XRC

Python (main.py) (layout.py)
#!/usr/bin/env python3
import wx.xrc

class MyApp(wx.App):
    def OnInit(self):
        res = wx.xrc.XmlResource('layout.xrc')
        frame = res.LoadObject(None, 'mainFrame', 'wxMDIParentFrame')
        frame.Show()
        return True

if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()
<?xml version="1.0" encoding="utf-8"?>
<resource version="2.9.0.5">
  <object name="mainFrame" class="wxMDIParentFrame">
    <title>Hello World!</title>
  </object>
</resource>