wxpython 控件API 实例一
时间:2010-04-11 来源:masonzhang
#-*- coding:cp936-*-
import wx
class ShowDia(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title)
panle = wx.Panel(self, size=(400, 400))
box = wx.BoxSizer(wx.VERTICAL)
panel_picture = wx.Panel(panle, -1, size=(320, 240), style=wx.SUNKEN_BORDER)
self.picture = wx.StaticBitmap(panel_picture)
panel_picture.SetBackgroundColour(wx.WHITE)
box.Add(panel_picture, 1, wx.ALL | wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 10)
self.imagelist = ['PLMM1.jpg', 'PLMM2.jpg', 'PLMM3.jpg', 'PLMM4.jpg', 'PLMM5.jpg', 'PLMM6.jpg', 'PLMM7.jpg', 'PLMM8.jpg']
self.author = ['邻家女优', '楚楚动人', '时尚精灵', '时尚女', '败家女', '可爱', '淘气', '老婆女']
SCom = wx.ComboBox(panle, -1, choices=self.author, style=wx.CB_READONLY)
button = wx.Button(panle, -1, 'Close')
box.Add(SCom, 0, wx.ALIGN_CENTER)
box.Add(button, 0, wx.ALIGN_RIGHT | wx.BOTTOM | wx.RIGHT, 10)
wx.EVT_BUTTON(self, button.GetId(), self.exit)
wx.EVT_COMBOBOX(self, SCom.GetId(), self.showpicture)
panle.SetSizer(box)
self.SetClientSize((320, 300))
self.Center()
self.ShowModal()
def exit(self, event):
self.Destroy()
def showpicture(self, event):
item = event.GetSelection()
self.SetFocus()
bitmapstr = self.imagelist[item]
bmp = wx.Image("d:\\icon\\" + bitmapstr)
x = bmp.Scale(320, 240)
self.picture.SetBitmap(wx.BitmapFromImage(x))
self.Refresh()
class Staticbox(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title)
self.SetMinSize((320, 240))
MainPanle = wx.Panel(self)
MainBox = wx.BoxSizer(wx.VERTICAL)
panle = wx.Panel(MainPanle);
box = wx.BoxSizer(wx.VERTICAL)
SBOXsizer = wx.StaticBoxSizer(wx.StaticBox(panle, -1, 'Option'), orient=wx.VERTICAL)
SCK1 = wx.CheckBox(panle, -1, 'One')
SCK2 = wx.CheckBox(panle, -1, 'Two')
SCK3 = wx.CheckBox(panle, -1, 'Three')
SLab = wx.StaticText(panle, -1, '范围选择1,120')
Spin = wx.SpinCtrl(panle, -1, '小可爱', min=1, max=120)
box.Add(SCK1, 0, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 10)
box.Add(SCK2, 0, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 10)
box.Add(SCK3, 0, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 10)
box.Add(SLab, 0, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 10)
box.Add(Spin, 0, wx.LEFT | wx.RIGHT | wx.TOP, 10)
SBOXsizer.Add(box, 0, wx.EXPAND, 10)
panle.SetSizer(SBOXsizer)
MainBox.Add(panle, 1, wx.ALL | wx.EXPAND, 10)
button = wx.Button(MainPanle, -1, 'OK')
MainBox.Add(button, 0, wx.ALIGN_RIGHT | wx.RIGHT | wx.BOTTOM, 10)
MainPanle.SetSizer(MainBox)
wx.EVT_BUTTON(self, button.GetId(), self.exit)
#=====show windows
self.Center()
self.SetClientSize((320, 240))
self.ShowModal()
self.Destroy()
def exit(self, event):
self.Destroy()
class MyFrame (wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
self.StatusBar = wx.StatusBar(self)
self.StatusBar.SetFieldsCount(2)
menuBar = wx.MenuBar()
file_menu = wx.Menu()
Dialog_menu = wx.Menu()
D1 = Dialog_menu.Append(-1, 'D1')
D2 = Dialog_menu.Append(-1, 'D2')
F1 = file_menu.Append(-1, 'quit')
#======event =================
wx.EVT_MENU(self, F1.GetId(), self.click_quit)
wx.EVT_MENU(self, D1.GetId(), self.ShowD1)
wx.EVT_MENU(self, D2.GetId(), self.ShowD2)
menuBar.Append(file_menu, '&File')
menuBar.Append(Dialog_menu, '&Dialog')
#=== add MenuBar
#Show windows
self.SetMinSize((480, 320))
self.SetMenuBar(menuBar)
self.Center()
self.Show()
def click_quit(self, event):
self.Close()
def ShowD1(self, event):
ShowDia(self, -1, 'D1')
pass
def ShowD2(self, event):
Staticbox(self, -1, 'D2')
pass
app = wx.PySimpleApp()
MyFrame(None, -1, 'D6.py')
app.MainLoop()
相关阅读 更多 +