wxPython实例一

来源:互联网 发布:博弈量能 知乎 编辑:程序博客网 时间:2024/05/21 14:47

效果:


代码:

import wxclass MyFrame(wx.Frame):        def __init__(self):        wx.Frame.__init__(self, None, -1, 'My Frame', size=(300, 300))        panel = wx.Panel(self, -1)        panel.Bind(wx.EVT_MOTION, self.OnMove)        wx.StaticText(panel, -1, 'Pos:', pos=(10, 12))        self.posCtrl = wx.TextCtrl(panel, -1, '', pos=(40, 10))            def OnMove(self, event):        pos = event.GetPosition()        self.posCtrl.SetValue('%s, %s' % (pos.x, pos.y))        if __name__ == '__main__':    app = wx.PySimpleApp()    frame = MyFrame()    frame.Show(True)    app.MainLoop()

效果:


代码:

# -*- coding: utf-8 -*-import wx #导入必须的wxPython包class App(wx.App): #子类化wxPython应用程序类    def OnInit(self): #定义一个应用程序的初始化方法,将被wx.App父类调用。        #定义了一个没有父亲的框架,它是一个顶级的框架        frame = wx.Frame(parent=None, title='Bare')        #给Show方法一个布尔值参数来设定frame的可见性,默认True        frame.Show()        return Trueapp = App() #创建一个应用程序类的实例app.MainLoop() #进入这个应用程序的主事件循环


效果:



代码:

# -*- coding: utf-8 -*-import wxclass Frame(wx.Frame): #wx.Frame的子类    def __init__(self, image, parent=None, id=-1,                    pos=wx.DefaultPosition,                    title='Hello, wxPython!'):        temp = image.ConvertToBitmap()        size = temp.GetWidth(), temp.GetHeight() #用图像的宽度和高度创建一个size元组        wx.Frame.__init__(self, parent, id, title, pos, size)        #用wx.StaticBitmap控件来显示这个图像,它要求一个位图        self.bmp = wx.StaticBitmap(parent=self, bitmap=temp)     class App(wx.App): #wx.App的子类,这是wxPython应用程序最基本的要求       def OnInit(self): #定义一个应用程序的初始化方法,将被wx.App父类调用。        image = wx.Image('wxPython.jpg', wx.BITMAP_TYPE_JPEG)        self.frame = Frame(image)        #给Show方法一个布尔值参数来设定frame的可见性,默认True        self.frame.Show()        #一个wxPython程序可以有几个框架,其中有一个是被设计为应用程序的顶级窗口的        self.SetTopWindow(self.frame)        #如果所返回的值是False,则应用程序将立即退出        return Trueif __name__ == '__main__':    app = App() #创建一个应用程序类的实例    app.MainLoop() #进入这个应用程序的主事件循环


应用程序对象的生命周期

效果:


代码:

# -*- coding: utf-8 -*-import wximport sysclass Frame(wx.Frame):    def __init__(self, parent, id, title):        print 'Frame __init__'        wx.Frame.__init__(self, parent, id, title)    class App(wx.App):    def __init__(self, redirect=True, filename=None):        print 'App __init__'        wx.App.__init__(self, redirect, filename)            def OnInit(self):        print 'OnInit' #输出到stdout        self.frame = Frame(parent=None, id=-1, title='Startup') #创建框架        self.frame.Show()        self.SetTopWindow(self.frame)        print >> sys.stderr, 'A pretend error message' #输出到stderr        return True    def OnExit(self):        print 'OnExit'        if __name__ == '__main__':    #标准输出流sys.stdout和标准错误流sys.stderr是否重定向    #输出到控制台    #app = App(redirect=False)    #输出到filename所指定的文件中    #app = App(redirect=True, filename='output')    #应用程序创建了一个空的框架和生成了一个用于重定向输出的框架    app = App(redirect=True)        print 'before MainLoop'    app.MainLoop() #2 进入主事件循环    print 'after MainLoop'

效果:


代码:

# -*- coding: utf-8 -*-import wximport wx.py.images as imagesclass ToolbarFrame(wx.Frame):    def __init__(self, parent, id):        wx.Frame.__init__(self, parent, id, 'Toolbars', size=(500, 400))        self.panel = wx.Panel(self)        self.panel.SetBackgroundColour('White')        self.statusBar = self.CreateStatusBar() #创建状态栏        self.toolbar = self.CreateToolBar() #创建工具栏        self.toolbar.AddSimpleTool(wx.NewId(), images.getPyBitmap(), 'New', 'Long help for "New"') #给工具栏增加一个工具        self.toolbar.Realize() #准备显示工具栏        self.menuBar = wx.MenuBar() #创建菜单栏        # 创建两个菜单        menu1 = wx.Menu()        self.menuBar.Append(menu1, '&File')        menu2 = wx.Menu()        #创建菜单的项目        menu2.Append(wx.NewId(), '&Copy', 'Copy in status bar')        menu2.Append(wx.NewId(), 'C&ut', '')        menu2.Append(wx.NewId(), 'Paste', '')        menu2.AppendSeparator()        menu2.Append(wx.NewId(), '&Options...', 'Display Options')        self.menuBar.Append(menu2, '&Edit') # 在菜单栏上附上菜单        self.SetMenuBar(self.menuBar) #在框架上附上菜单栏        if __name__ == '__main__':    app = wx.PySimpleApp()    frame = ToolbarFrame(parent=None, id=-1)    frame.Show()    app.MainLoop()        


效果:


代码:

# -*- coding: utf-8 -*-import wxif __name__ == '__main__':    app = wx.PySimpleApp()    dialog = wx.MessageDialog(None, 'Is this the coolest thing ever!',                            'MessageDialog', wx.OK | wx.ICON_QUESTION | wx.CANCEL)    #将对话框以模式框架的方式显示,这意味着在对话框关闭之前,    #应用程序中的别的窗口不能响应用户事件.对于wx.MessageDialog,返回值    #是下面常量之一: wx.ID_YES, wx.ID_NO, wx.ID_CANCEL, wx.ID_OK    result = dialog.ShowModal()    dialog.Destroy()    app.MainLoop()            


效果:


代码:

# -*- coding: utf-8 -*-import wxif __name__ == '__main__':    app = wx.PySimpleApp()       dlg = wx.TextEntryDialog(None, "Who is buried in Grant's tomb?", 'A Question', 'Cary Grant')    if dlg.ShowModal() == wx.ID_OK:        response = dlg.GetValue()        print response


效果:


# -*- coding: utf-8 -*-import wxif __name__ == '__main__':    app = wx.PySimpleApp()     dlg = wx.SingleChoiceDialog(None, 'What version of Python are you using?',                                 'Single Choice', ['1.5.2', '2.0', '2.1.3', '2.2', '2.3.1'])    if dlg.ShowModal() == wx.ID_OK:        index = dlg.GetSelection()#返回用户选项的索引        response = dlg.GetStringSelection()#返回实际所选的字符串        print index, ': ', response



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 去韩国打工签证怎么办多么钱 厕所堵了怎么办有妙招 蹲式厕所老是堵怎么办 下蹲式厕所堵了怎么办 蹲的厕所堵住了怎么办 厕所通了又堵怎么办 蹲式厕所经常堵怎么办 厕所下水管堵了怎么办 厕所堵了水满了怎么办 拉屎把厕所堵了怎么办 厕所堵了不下水怎么办 坐厕所堵了怎么办妙招 火车上丢了东西怎么办 网购的东西丢了怎么办 在酒店丢了东西怎么办 我好懒不想工作怎么办 被宠物刺猬咬了怎么办 被老鼠咬了怎么办打针 手指被老鼠咬了怎么办 孕妇被老鼠咬了怎么办 耳朵里面一直嗡嗡响怎么办 把语言栏删了怎么办 乐视会员到期了怎么办 预提费用取消了怎么办 小学生上课注意力不集中怎么办 工商抽查到你了怎么办 拿到商调函后该怎么办 苹果7p掉水里了怎么办 苹果7屏幕进水了怎么办 苹果5s掉进水里怎么办 苹果5s无法开机怎么办 苹果手机充不了电怎么办 苹果5s掉了怎么办 苹果6手机掉水里了怎么办 苹果5s关机丢了怎么办 苹果5s发热严重怎么办 手机开不开机了怎么办 苹果6基带坏了怎么办 苹果7基带坏了怎么办 苹果刷机1错误怎么办 4s解锁密码忘了怎么办