wxPython入门练习代码 二
2016-08-30 23:29
369 查看
WxPython书籍[摘记]
1.任何wxPython应用程序都需要一个应用程序对象。这个应用程序对象必须是类wx.App或其定制的子类的一个实例。
2.在OnInit()方法中将至少创建一个框架对象,并调用该框架的Show()方法。
3.如果在系统中只有一个框架的话,避免创建一个wx.App子类。
4.如果你的应用程序十分简单的话,你应该只使用wx.PySimpleApp,且不需要任何其它的全局参数。
5.wxPython应用程序将保持存活直到全局函数wx.Exit()被明确地调用。
6.一个应用程序一次只能有一主顶级窗口。
7.顶级窗口对象通常是类wx.Frame的子类,尽管它也可以是wx.Dialog的子类。
8.wxPython中的说法,框架就是用户通常称的窗口。在wxPython中,wx.Frame是所有框架的父类。
运行 python hello.py 出错,提示:
File "<stdin>" , line 1
python hello.py
解释:
In the shell you can run shell commands, in the Python command line you can run Python code.
"python hello.py" is a shell command, not Python code, so you should run it in the shell, but not on the Python command line.
HellowxPython.py:
AppFrame.py:
1.任何wxPython应用程序都需要一个应用程序对象。这个应用程序对象必须是类wx.App或其定制的子类的一个实例。
2.在OnInit()方法中将至少创建一个框架对象,并调用该框架的Show()方法。
3.如果在系统中只有一个框架的话,避免创建一个wx.App子类。
4.如果你的应用程序十分简单的话,你应该只使用wx.PySimpleApp,且不需要任何其它的全局参数。
5.wxPython应用程序将保持存活直到全局函数wx.Exit()被明确地调用。
6.一个应用程序一次只能有一主顶级窗口。
7.顶级窗口对象通常是类wx.Frame的子类,尽管它也可以是wx.Dialog的子类。
8.wxPython中的说法,框架就是用户通常称的窗口。在wxPython中,wx.Frame是所有框架的父类。
运行 python hello.py 出错,提示:
File "<stdin>" , line 1
python hello.py
解释:
In the shell you can run shell commands, in the Python command line you can run Python code.
"python hello.py" is a shell command, not Python code, so you should run it in the shell, but not on the Python command line.
HellowxPython.py:
#!/user/bin/env python """Hello,wxPython!Program.""" import wx #自定义子类化Frame class 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() wx.Frame.__init__(self,parent,id,title,pos,size) #wx.StaticBitmap显示位图 self.bmp = wx.StaticBitmap(parent=self,bitmap=temp) class App(wx.App): def OnInit(self): image = wx.Image('wxPython.jpg',wx.BITMAP_TYPE_JPEG) self.frame = Frame(image) self.frame.Show() self.SetTopWindow(self.frame) return True def main(): app = App() app.MainLoop() if __name__ == '__main__': main()
AppFrame.py:
#!/usr/bin/env python import wx #import images class AppFrame(wx.Frame): def __init__(self,parent,id): wx.Frame.__init__(self,parent,id,'AppFrame',size=(300,300)) #1.Create Frame Panel............ panel = wx.Panel(self) #2.Set Panel BackgroundColor........ panel.SetBackgroundColour('White') #3.Create Buttons........ button = wx.Button(panel,label="CloseButton",pos=(125,10),size=(100,50)) buttonMsg = wx.Button(panel,label="MsgButton",pos=(125,60),size=(100,50)) #4.Buttons Bind Self EventFunctions...... self.Bind(wx.EVT_BUTTON,self.OnCloseMe,button) self.Bind(wx.EVT_BUTTON,self.OnMsgMe,buttonMsg) self.Bind(wx.EVT_CLOSE,self.OnCloseWindow) #5.Create Frame StatusBar...... statusBar = self.CreateStatusBar() #6.Create Frame ToolBar......... #toolBar = self.CreateToolBar() #toolBar.AddSimpleTool(wx.NewId(),images.getNewBitmap(),"New", # "Long help for 'New'") #toolBar.Realize() #7.Create Frame MenuBar...... menuBar = wx.MenuBar() menu1 = wx.Menu() menu1.Append(wx.NewId(),"&Open","Open in status bar") menu1.Append(wx.NewId(),"&Close","Close in status bar") menuBar.Append(menu1,"&File") menu2 = wx.Menu() menu2.Append(wx.NewId(),"&Copy","Copy in status bar") menu2.Append(wx.NewId(),"&Cut","") menu2.Append(wx.NewId(),"&Paste","") menu2.AppendSeparator() menu2.Append(wx.NewId(),"&Options","Display Options") menuBar.Append(menu2,"&Edit") self.SetMenuBar(menuBar) def OnCloseMe(self,event): self.Close(True) def OnCloseWindow(self,event): self.Destroy() def OnMsgMe(self,event): dlg = wx.MessageDialog(None,'This is test!','MsgDialog',wx.YES_NO|wx.ICON_QUESTION) result = dlg.ShowModal() dlg.Destroy() if __name__ == '__main__': app = wx.PySimpleApp() frame = AppFrame(parent=None,id=-1) frame.Show() app.MainLoop()
相关文章推荐
- wxPython入门练习代码 一
- wxPython入门练习代码 三
- wxPython入门练习代码 四
- selenium入门练习01代码补充
- 算法入门经典第5章,练习代码
- 算法竞赛入门经典 第二章 上机练习(C++代码)
- Java——I/O入门相关练习代码
- Python(编程小白的第一本 Python 入门书) 学习笔记2——代码练习
- 算法竞赛入门经典,练习代码 ,3.4.9 上机练习
- python入门代码练习
- 【day 9】python编程:从入门到实践学习笔记-测试代码(末附练习答案代码)
- 第五届蓝桥杯练习题 - 入门练习Java解题代码
- 【day 4】python编程:从入门到实践学习笔记-字典(末附练习答案代码)
- 我的JAVA练习代码
- 学Shellcode 的练习代码
- 操作系统入门(五) - 载入32位保护模式代码为c做准备
- [推荐]ASP.NET 2.0 快速入门系列课程(附代码示例)[WMV]
- 新手入门常用代码集锦
- C#入门代码
- C#入门代码