顯示具有 wxPython 標籤的文章。 顯示所有文章
顯示具有 wxPython 標籤的文章。 顯示所有文章

2010年10月22日 星期五

wxPython 筆記 ( event 01 )

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx

class MyApp(wx.App):
    def OnInit(self):
        frame=MyFrame(title=u"視窗",pos=(100,150),size=(250,200))
        frame.Show()
        self.SetTopWindow(frame)
        return True


class MyFrame(wx.Frame):
    def __init__(self,title,pos,size):
        wx.Frame.__init__(self,None,-1,title,pos,size) # 備用,使用取代 frame 的 OnInit()

        #建立panel
        panel=wx.Panel(self)
        #新增一個按鈕並把它加到panel裡面
        button=wx.Button(panel,label=u"按鍵_1",pos=(25,25),size=(80,50))
        #新增另一個按鈕並把它加到panel裡面
        button_2=wx.Button(panel,label=u"按鍵_2",pos=(25,100),size=(80,50))

        # 建立事件監聽 #############################################################

        # 攔截「右上角 x 之視窗關閉鍵   wx.EVT_CLOSE」,轉向 OnCloseWin,得 :確定要關閉程式
        self.Bind(wx.EVT_CLOSE,self.OnCloseWin)

        #攔截「按 button 鍵   wx.EVT_BUTTON 」,轉向 OnClick ,得出 : 你按了 '按鍵1'
        self.Bind(wx.EVT_BUTTON,self.OnClick_1,button)

        #攔截「按 button 鍵   wx.EVT_BUTTON 」,轉向 OnClick ,得出 : 你按了 '按鍵1'
        self.Bind(wx.EVT_BUTTON,self.OnClick_2,button_2)

    #定義按鈕案下的函數  
    def OnClick_1(self,event):
        wx.MessageBox(u"你按了 '按鍵_1' ",u"提示訊息")

    def OnClick_2(self,event):
        wx.MessageBox(u"你按了 '按鍵_2' ",u"提示訊息")
      
    #定義按下關閉視窗(右上X)的函數
    def OnCloseWin(self,event):
        dlg=wx.MessageDialog(None,u"確定要關閉程式?",u"提示訊息",wx.YES_NO)
      
        #按下YES才會關閉視窗
        if dlg.ShowModal()==wx.ID_YES:
            self.Destroy()


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

2010年10月13日 星期三

wxPython 筆記 (calculator)
















#!/usr/bin/python
# http://wiki.wxpython.org/AnotherTutorial
# calculator.py

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):

        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(300, 250))
        self.formula = False
        menubar = wx.MenuBar()
        file = wx.Menu()
        file.Append(22, '&Quit', 'Exit Calculator')
        menubar.Append(file, '&File')
        self.SetMenuBar(menubar)
        wx.EVT_MENU(self, 22, self.OnClose)
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.display = wx.TextCtrl(self, -1, '',  style=wx.TE_RIGHT)
        sizer.Add(self.display, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 4)

        gs = wx.GridSizer(4, 4, 3, 3)
        gs.AddMany([(wx.Button(self, 20, 'Cls'), 0, wx.EXPAND),
                        (wx.Button(self, 21, 'Bck'), 0, wx.EXPAND),
                        (wx.StaticText(self, -1, ''), 0, wx.EXPAND),
                        (wx.Button(self, 22, 'Close'), 0, wx.EXPAND),
                        (wx.Button(self, 1, '7'), 0, wx.EXPAND),
                        (wx.Button(self, 2, '8'), 0, wx.EXPAND),
                        (wx.Button(self, 3, '9'), 0, wx.EXPAND),
                        (wx.Button(self, 4, '/'), 0, wx.EXPAND),
                        (wx.Button(self, 5, '4'), 0, wx.EXPAND),
                        (wx.Button(self, 6, '5'), 0, wx.EXPAND),
                        (wx.Button(self, 7, '6'), 0, wx.EXPAND),
                        (wx.Button(self, 8, '*'), 0, wx.EXPAND),
                        (wx.Button(self, 9, '1'), 0, wx.EXPAND),
                        (wx.Button(self, 10, '2'), 0, wx.EXPAND),
                        (wx.Button(self, 11, '3'), 0, wx.EXPAND),
                        (wx.Button(self, 12, '-'), 0, wx.EXPAND),
                        (wx.Button(self, 13, '0'), 0, wx.EXPAND),
                        (wx.Button(self, 14, '.'), 0, wx.EXPAND),
                        (wx.Button(self, 15, '='), 0, wx.EXPAND),
                        (wx.Button(self, 16, '+'), 0, wx.EXPAND) ])

        sizer.Add(gs, 1, wx.EXPAND)

        self.SetSizer(sizer)
        self.Centre()

        self.Bind(wx.EVT_BUTTON, self.OnClear, id=20)
        self.Bind(wx.EVT_BUTTON, self.OnBackspace, id=21)
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=22)
        self.Bind(wx.EVT_BUTTON, self.OnSeven, id=1)
        self.Bind(wx.EVT_BUTTON, self.OnEight, id=2)
        self.Bind(wx.EVT_BUTTON, self.OnNine, id=3)
        self.Bind(wx.EVT_BUTTON, self.OnDivide, id=4)
        self.Bind(wx.EVT_BUTTON, self.OnFour, id=5)
        self.Bind(wx.EVT_BUTTON, self.OnFive, id=6)
        self.Bind(wx.EVT_BUTTON, self.OnSix, id=7)
        self.Bind(wx.EVT_BUTTON, self.OnMultiply, id=8)
        self.Bind(wx.EVT_BUTTON, self.OnOne, id=9)
        self.Bind(wx.EVT_BUTTON, self.OnTwo, id=10)
        self.Bind(wx.EVT_BUTTON, self.OnThree, id=11)
        self.Bind(wx.EVT_BUTTON, self.OnMinus, id=12)
        self.Bind(wx.EVT_BUTTON, self.OnZero, id=13)
        self.Bind(wx.EVT_BUTTON, self.OnDot, id=14)
        self.Bind(wx.EVT_BUTTON, self.OnEqual, id=15)
        self.Bind(wx.EVT_BUTTON, self.OnPlus, id=16)

    def OnClear(self, event):
        self.display.Clear()

    def OnBackspace(self, event):
        formula = self.display.GetValue()
        self.display.Clear()
        self.display.SetValue(formula[:-1])

    def OnClose(self, event):
        self.Close()

    def OnDivide(self, event):
        if self.formula:
            return
        self.display.AppendText('/')

    def OnMultiply(self, event):
        if self.formula:
            return
        self.display.AppendText('*')

    def OnMinus(self, event):
        if self.formula:
            return
        self.display.AppendText('-')

    def OnPlus(self, event):
        if self.formula:
            return
        self.display.AppendText('+')

    def OnDot(self, event):
        if self.formula:
            return
        self.display.AppendText('.')

    def OnEqual(self, event):
        if self.formula:
            return
        formula = self.display.GetValue()
        self.formula = False
        try:
            self.display.Clear()
            output = eval(formula)
            self.display.AppendText(str(output))
        except StandardError:
            self.display.AppendText("Error")

    def OnZero(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('0')

    def OnOne(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('1')

    def OnTwo(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('2')

    def OnThree(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('3')

    def OnFour(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('4')

    def OnFive(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('5')

    def OnSix(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('6')

    def OnSeven(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('7')

    def OnEight(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('8')

    def OnNine(self, event):
        if self.formula:
            self.display.Clear()
            self.formula = False
        self.display.AppendText('9')

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'calculator.py')
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

app = MyApp(0)
app.MainLoop()
  
  
  

2010年10月12日 星期二

wxPython 筆記 (RadioBox)

import wx

class RadioBoxFrame(wx.Frame):
#    def __init__(self):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id, 'Radio Box Example', size=(550, 400))

        panel = wx.Panel(self)
       
        sampleList = ['zero', 'one', 'two', 'three', 'four', 'five','six', 'seven', 'eight']
        wx.RadioBox(panel, -1, "A Radio Box", (10, 10), wx.DefaultSize,sampleList, 2, wx.RA_SPECIFY_COLS)
        wx.RadioBox(panel, -1, "Next Radio Box", (150, 10), wx.DefaultSize,sampleList, 3, wx.RA_SPECIFY_COLS | wx.NO_BORDER)
        wx.RadioBox(panel, -1, "Thrid Radio Box", (350, 10), wx.DefaultSize,sampleList, 1, wx.RA_SPECIFY_COLS | wx.NO_BORDER)
        wx.RadioBox(panel, -1, "Fourth Radio Box", (10, 250), wx.DefaultSize,sampleList, 1, wx.RA_SPECIFY_ROWS | wx.NO_BORDER)

if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=RadioBoxFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

2010年10月11日 星期一

創建一個wx.App 的子類

[摘錄] 創建一個 wx.App 的子類
http://www.czug.org/python/wxpythoninaction/ch02.htm

創建和使用一個wx.App子類,你需要執行四個步驟:
1、定義這個子類
2、在定義的子類中寫一個OnInit()方法
3、在你的程式的主要部分創建這個類的一個實例
4、調用應用程式實例的MainLoop()方法。這個方法將程式的控制權轉交給wxPython 

import wx 
class MyApp(wx.App):   #1 定義子類
def OnInit(self):     #2 OnInit()方法
    frame = wx.Frame(parent=None, title=u'這是 Frame 容器')
    frame.Show()
    return True
app = MyApp()          #3 創建這個類的一個實例
app.MainLoop()         #4調用MainLoop() 方法

我們在第一章中看到過OnInit()方法。它在應用程式開始時並在主事件迴圈開始前被wxPython系統調用。這個方法不要求參數並返回一個布林值,如果所返回的值是False,則應用程式將立即退出。大多數情況下,你將想要該方法返回的結果為真。處理某些錯誤條件,退出可能是恰當的方法,諸如所一個所需的資源缺失。
 
由於OnInit()方法的存在,並且它是wxPython架構的一部分,所以任何關於你的定制的類的所需的初始化通常都由OnInit()方法管理,而不在Python__init__方法中。

如果由於某些原因你決定需要__init__方法,那麼你必須在你的__init__方法中調用父類的__init__方法,如下所示:
 
wx.App.__init__(self)

通常,你在OnInit()方法中將至少創建一個框架物件,並調用該框架的Show()方法。你也可以有選擇地通過調用SetTopWindow()方法來為應用程式指定一個框架作為頂級視窗。頂級視窗被作為那些沒有指定父視窗的對話方塊的默認父視窗。

 

2010年10月5日 星期二

wxPython 筆記 01

#############################################################
# Sample01 最簡單的 window
#############################################################

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

import wx #02

app = wx.PySimpleApp() #03

frame = wx.Frame(None, -1, u"這是 Frame 容器") #04
frame.Show() #05

app.MainLoop() #06

#01 定為 unicode 支援中文
#02 載入 wx 提供的資源
#03 通過使用 wxPySimpleApp 類創建一個應用程序對像:(佔用一定的記憶體)。
#04 創造一個 wxFrame 的實例, parent = None , id = -1, title = u'這是 Frame 容器'
#05 顯示剛創建的這個框框
#06 指定 wxPysimpleApp 在畫面上循環顯示,等待輸入。


#############################################################
# 備忘 wx.Frame(None, -1, "這是 Frame 容器")
#############################################################


wxPython -- wx.Frame參數說明
http://putidea.blogspot.com/2008/10/wxpython-wxframe.html

wxPython -- wxFrame style設定語法
http://putidea.blogspot.com/2008/10/wxpython-wxframe-style.html

wxPython -- ID的使用
http://putidea.blogspot.com/2008/10/wxpython-id.html



#############################################################
# Sample02 稍稍進階,標準的 window
#############################################################


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

import wx

class MyFirstFrame(wx.Frame): #07
def __init__(self,parent,id): #08
wx.Frame.__init__(self,parent,id,u'這是 Frame 容器',size=(500,300))

if __name__=='__main__': #09
app=wx.PySimpleApp()
frame=MyFirstFrame(parent=None,id=-1)
frame.Show()
app.MainLoop()

#07 以 class 創造 frame
#08 frame 的初始化
#09 確認本 frame 不是被引用時



###############################################################
# Sample03 另一個標準例子
# http://www.czug.org/python/wxpythoninaction/ch01.htm
###############################################################

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

import wx #10

class MyApp(wx.App): #11
def OnInit(self): #11-01
frame = wx.Frame(parent=None, title=u'這是 Frame 容器')
frame.Show()
return True

app = MyApp() #12
app.MainLoop() #13

#10 導入 wxPython
#11 引用 wxPython 的類、函數和常量(均以wx為前綴)
#11-01 定義 OnInit 為一個應用程序及初始化
#12 創建一個應用程序類的實例
#13 進入這個應用程序的主事件循環