您的位置:首页 > 编程语言 > Python开发

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:

#!/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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: