• wxPython入门练习代码 二


    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:

     1 #!/user/bin/env python
     2 
     3 """Hello,wxPython!Program."""
     4 
     5 import wx
     6 
     7 #自定义子类化Frame
     8 class Frame(wx.Frame):
     9     def __init__(self,image,parent=None,id=-1,
    10                 pos=wx.DefaultPosition,
    11                 title='Hello,wxPython!'):
    12         temp = image.ConvertToBitmap()
    13         size = temp.GetWidth(),temp.GetHeight()
    14         wx.Frame.__init__(self,parent,id,title,pos,size)
    15         #wx.StaticBitmap显示位图
    16         self.bmp = wx.StaticBitmap(parent=self,bitmap=temp)
    17         
    18 class App(wx.App):
    19     def OnInit(self):
    20         image = wx.Image('wxPython.jpg',wx.BITMAP_TYPE_JPEG)
    21         self.frame = Frame(image)
    22         
    23         self.frame.Show()
    24         self.SetTopWindow(self.frame)
    25         return True
    26 
    27 def main():
    28     app = App()
    29     app.MainLoop()
    30     
    31 if __name__ == '__main__':
    32     main()
    33         

    AppFrame.py:

     1 #!/usr/bin/env python
     2 
     3 import wx
     4 #import images
     5 
     6 class AppFrame(wx.Frame):
     7     def __init__(self,parent,id):
     8         wx.Frame.__init__(self,parent,id,'AppFrame',size=(300,300))
     9         #1.Create Frame Panel............
    10         panel = wx.Panel(self)
    11         #2.Set Panel BackgroundColor........
    12         panel.SetBackgroundColour('White')
    13         #3.Create Buttons........
    14         button = wx.Button(panel,label="CloseButton",pos=(125,10),size=(100,50))
    15         buttonMsg = wx.Button(panel,label="MsgButton",pos=(125,60),size=(100,50))
    16         #4.Buttons Bind Self EventFunctions......
    17         self.Bind(wx.EVT_BUTTON,self.OnCloseMe,button)
    18         self.Bind(wx.EVT_BUTTON,self.OnMsgMe,buttonMsg)
    19         self.Bind(wx.EVT_CLOSE,self.OnCloseWindow)
    20         #5.Create Frame StatusBar......
    21         statusBar = self.CreateStatusBar()
    22         #6.Create Frame ToolBar.........
    23         #toolBar = self.CreateToolBar()
    24         #toolBar.AddSimpleTool(wx.NewId(),images.getNewBitmap(),"New",
    25         #         "Long help for 'New'")
    26         #toolBar.Realize()
    27         #7.Create Frame MenuBar......
    28         menuBar = wx.MenuBar()
    29         menu1 = wx.Menu()
    30         menu1.Append(wx.NewId(),"&Open","Open in status bar")
    31         menu1.Append(wx.NewId(),"&Close","Close in status bar")
    32         menuBar.Append(menu1,"&File")
    33         menu2 = wx.Menu()
    34         menu2.Append(wx.NewId(),"&Copy","Copy in status bar")
    35         menu2.Append(wx.NewId(),"&Cut","")
    36         menu2.Append(wx.NewId(),"&Paste","")
    37         menu2.AppendSeparator()
    38         menu2.Append(wx.NewId(),"&Options","Display Options")
    39         menuBar.Append(menu2,"&Edit")
    40         self.SetMenuBar(menuBar)
    41         
    42     def OnCloseMe(self,event):
    43         self.Close(True)
    44         
    45     def OnCloseWindow(self,event):
    46         self.Destroy()
    47     
    48     def OnMsgMe(self,event):
    49         dlg = wx.MessageDialog(None,'This is test!','MsgDialog',wx.YES_NO|wx.ICON_QUESTION)
    50         result = dlg.ShowModal()
    51         dlg.Destroy()
    52         
    53 if __name__ == '__main__':
    54     app = wx.PySimpleApp()
    55     frame = AppFrame(parent=None,id=-1)
    56     frame.Show()
    57     app.MainLoop()
    58         
    59         
  • 相关阅读:
    程序员是怎么炼成的---OC题集--练习答案与题目(3)
    程序员是怎么炼成的---OC题集--练习答案与题目(2)
    152. Maximum Product Subarray
    151. Reverse Words in a String
    150. Evaluate Reverse Polish Notation
    148. Sort List
    147. Insertion Sort List
    145. Binary Tree Postorder Traversal
    144. Binary Tree Preorder Traversal
    140. Word Break II
  • 原文地址:https://www.cnblogs.com/ljfy-yjw/p/5824131.html
Copyright © 2020-2023  润新知