ReadEasy

20 10月, 2012

wxpython sample

wxPython是一個wrapper提供給python的一項wxWidgets包裝

可以輕輕鬆鬆用Python就畫出跨平台的GUI介面

若你有OOP概念基礎 ,相信會十分好上手

最簡易的雛型 wx.App -> wx.Frame
import wx

class frame(wx.Frame):
    def __init__(self):
        f = wx.Frame.__init__(self,parent=None, title='xxx')
        self.Show()

if __name__ == '__main__':
    gui = wx.App()
    gui.Frame = frame()
    gui.MainLoop()




一層一層往下建立 ,
wx.App -> Frame -> Panel -> object(button, textbox, label...etc) , sizer


#-*- coding: utf-8 -*-

import wx

class x(wx.Frame):
    def __init__(self):
        self.frame = wx.Frame.__init__(self, None, title='x', size=(250,250) )
        self.panel = xpanel(self)

    # if [app.Frame = x()] is disabled \
    #   and [f=x(),f.Show(True)] are enabled ,
    #   self.Show() should be disabled.
        self.Show()

class xpanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        self._chbox()
        self._procbar()
        self._cbtn()
        self._text()

    def _chbox(self):
        self.chbox = wx.CheckBox(self, -1, '1337', pos=(10, 90), size=wx.DefaultSize)

    def _procbar(self):
        self.cc = 0

        self.gauge = wx.Gauge(self, -1, range=50, pos=(10, 50), size=(220, 25))
        self.gauge.SetBezelFace(3)
        self.gauge.SetShadowWidth(3)
        self.gauge.Bind(wx.EVT_IDLE, self._idle)   

    def _idle(self, evt):
        self.cc = self.cc + 1
        if self.cc >= 50:
            self.cc = 0
        self.gauge.SetValue(self.cc)        

    def _text(self, w='null'):
        wx.StaticText(self, -1, 'null', (10,10) )
        self.tbox = wx.TextCtrl (self, -1,  w , (50,10) )

    def _cbtn(self):
        btn = wx.Button(self, label='T_T', pos=(100,100), size=(50,50) )
        #self.Bind(wx.EVT_BUTTON, self._print, btn)
        btn.Bind(wx.EVT_BUTTON, self._print)

    def _print(self, evt):
        print 'x'
        print self.tbox.GetValue()

        #print self.chbox.GetValue()
        print self.chbox.IsChecked()

        #msg = wx.MessageDialog(None, u'安安', 'title', wx.OK)
        #r   = msg.ShowModal()

        if self.chbox.IsChecked() == True : 
            wx.MessageBox(u'安安', 'title', wx.OK)
        else:
            print 'Not Checked'

app = wx.App(redirect=True)
#f = x()
#f.Show(True)
app.Frame = x()
app.MainLoop()


Refernce:

wxPython in action (網路上有很多相關的中文譯本資源 , 若有需要請自行Google) http://zetcode.com/wxpython/

沒有留言:

張貼留言