Python/wxPython/QuickStart: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
(Created page with "= Sksleton = == wxFrame == == wxMDIParentFrame == == wxDialog ==")
 
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Sksleton =
= Skeleton =
== wxFrame ==
== wxFrame without XRC ==
<source lang="python">
#!/usr/bin/env python3
import wx


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


== wxDialog ==
if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()
</source>
 
== wxFrame with XRC ==
{| class="wikitable"
|-
! Python (main.py) || XRC (layout.xrc)
|-
| valign="top" |
<source lang="python">
#!/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()
</source>
| valign="top" |
<source lang="xml">
<?xml version="1.0" encoding="utf-8"?>
<resource version="2.9.0.5">
  <object name="mainFrame" class="wxFrame">
    <title>Hello World!</title>
  </object>
</resource>
</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 ==
<source lang="python">
#!/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()
</source>
 
== wxMDIParentFrame with XRC ==
{| class="wikitable"
|-
! Python (main.py) || (layout.py)
|-
| valign="top" |
<source lang="python">
#!/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()
</source>
| valign="top" |
<source lang="xml">
<?xml version="1.0" encoding="utf-8"?>
<resource version="2.9.0.5">
  <object name="mainFrame" class="wxMDIParentFrame">
    <title>Hello World!</title>
  </object>
</resource>
</source>
|}

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>