wxPython编程

触发其他控件的事件

        panel=self.GetChildren()[0]
        for each in panel.GetChildren():
            if each.GetLabel() == "mybutton":
                finder=each
                break
        event=wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED,
                              finder.GetId())
        finder.GetEventHandler().ProcessEvent(event)


添加菜单栏

    def menuData(self):
        return [("&File",
                 (("&New", "New FIle", self.OnNew),("&Close", "Close FIle", self.OnClose))
                 ),
                ("&Option",
                 (("&Set", "Set Help", self.OnNew),("&Add", "Add Help", self.OnClose))
                 )
                ]
    def createMenuBar(self):
        menuBar = wx.MenuBar()
        for eachMenuData in self.menuData():
            menuLabel = eachMenuData[0]
            menuItems = eachMenuData[1]
            menuBar.Append(self.createMenu(menuItems), menuLabel)
        self.SetMenuBar(menuBar)
    def createMenu(self, menuData):
        menu = wx.Menu()
        for eachItem in menuData:
            if len(eachItem) == 2:
                label = eachItem[0]
                subMenu = self.createMenu(eachItem[1])
                menu.AppendMenu(wx.NewId(), label, subMenu)
            else:
                self.createMenuItem(menu,*eachItem)
        return menu
            
    def createMenuItem(self,menu,label,status,handler,
                       kind=wx.ITEM_NORMAL):
        if not label:
            menu.AppendSeparator()
            return
        menuItem=menu.Append(-1,label,status,kind)
        self.Bind(wx.EVT_MENU, handler, menuItem)
    def OnNew(self,event):
        wx.MessageBox("OnNew")
    def OnClose(self,event):
        wx.MessageBox("OnClose")

制作程序启动界面

        jpg=wx.Image("bk.jpg").ConvertToBitmap()
        wx.SplashScreen(jpg,wx.SPLASH_CENTER_ON_SCREEN|wx.SPLASH_TIMEOUT,
                        2000,None,-1)
        wx.Yield()


布局管理器


box布局

box=wx.BoxSizer(wx.HORIZONTAL)
box.Add(control1,1,wx.EXPAND)
box.Add(control2,2,wx.EXPAND)
self.SetSizer(box)


流式网格布局

sizer=wx.FlexGridSizer(cols=2,hgap=6,vgap=60)
        sizer.AddMany([staticText,basicText,pwdLabel])
        panel.SetSizer(sizer)



进度条

        self.gauge=wx.Gauge(panel,-1,100,(0,0),(300,200))
        self.gauge.SetBezelFace(3)
        self.gauge.SetShadowWidth(3)
        self.Bind(wx.EVT_IDLE, self.OnIdle)


运行wxpython自带的demo

把原有的

if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

替换成

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"TestFrame",size=(500,400))
        panel=TestPanel(self,None)


if __name__ == '__main__':
    app=wx.PySimpleApp()
    frame=MyFrame()
    frame.Show()
    app.MainLoop()


根据sizer自动设置初始窗口大小

fgs=wx.FlexGridSizer(cols=2,hgap=10,vgap=10)

p=wx.Panel(self)

p.SetSizerAndFit(fgs)

self.Fit()


使用剪切板

text_data=wx.TextDataObject("hello")
if wx.TheClipboard.Open():
    wx.TheClipboard.SetData(text_data)
    wx.TheClipboard.Flush()
    wx.TheClipboard.Close()

你可能感兴趣的:(编程,File,import,each,wxPython,menu)