[Python] 通过wxpython编写windows GUI程序 →→→→→进入此内容的聊天室

来自 , 2019-04-28, 写在 Python, 查看 116 次.
URL http://www.code666.cn/view/b7c0c7d6
  1. # using a wx.Frame, wx.MenuBar, wx.Menu, wx.Panel, wx.StaticText, wx.Button,
  2. # and a wx.BoxSizer to show a rudimentary wxPython Windows GUI application
  3. # wxPython package from: http://prdownloads.sourceforge.net/wxpython/
  4. # I downloaded: wxPython2.5-win32-ansi-2.5.3.1-py23.exe
  5. # if you have not already done so install the Python compiler first
  6. # I used  Python-2.3.4.exe  (the Windows installer package for Python23)
  7. # from http://www.python.org/2.3.4/
  8. # tested with Python23      vegaseat      24jan2005
  9. import wx
  10. class Frame1(wx.Frame):
  11.     # create a simple windows frame (sometimes called form)
  12.     # pos=(ulcX,ulcY)  size=(width,height) in pixels
  13.     def __init__(self, parent, title):
  14.         wx.Frame.__init__(self, parent, -1, title, pos=(150, 150), size=(350, 250))
  15.         # create a menubar at the top of the user frame
  16.         menuBar = wx.MenuBar()
  17.         # create a menu ...
  18.         menu = wx.Menu()
  19.         # ... add an item to the menu
  20.         # \tAlt-X creates an accelerator for Exit (Alt + x keys)
  21.         # the third parameter is an optional hint that shows up in
  22.         # the statusbar when the cursor moves across this menu item
  23.         menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit the program")
  24.         # bind the menu event to an event handler, share QuitBtn event
  25.         self.Bind(wx.EVT_MENU, self.OnQuitButton, id=wx.ID_EXIT)
  26.         # put the menu on the menubar
  27.         menuBar.Append(menu, "&File")
  28.         self.SetMenuBar(menuBar)
  29.         # create a status bar at the bottom of the frame
  30.         self.CreateStatusBar()
  31.         # now create a panel (between menubar and statusbar) ...
  32.         panel = wx.Panel(self)
  33.         # ... put some controls on the panel
  34.         text = wx.StaticText(panel, -1, "Hello World!")
  35.         text.SetFont(wx.Font(24, wx.SCRIPT, wx.NORMAL, wx.BOLD))
  36.         text.SetSize(text.GetBestSize())
  37.         quitBtn = wx.Button(panel, -1, "Quit")
  38.         messBtn = wx.Button(panel, -1, "Message")
  39.         # bind the button events to event handlers
  40.         self.Bind(wx.EVT_BUTTON, self.OnQuitButton, quitBtn)
  41.         self.Bind(wx.EVT_BUTTON, self.OnMessButton, messBtn)
  42.         # use a sizer to layout the controls, stacked vertically
  43.         # with a 10 pixel border around each
  44.         sizer = wx.BoxSizer(wx.VERTICAL)
  45.         sizer.Add(text, 0, wx.ALL, 10)
  46.         sizer.Add(quitBtn, 0, wx.ALL, 10)
  47.         sizer.Add(messBtn, 0, wx.ALL, 10)
  48.         panel.SetSizer(sizer)
  49.         panel.Layout()
  50.     def OnQuitButton(self, evt):
  51.         # event handler for the Quit button click or Exit menu item
  52.         print "See you later alligator! (goes to stdout window)"
  53.         wx.Sleep(1)    # 1 second to look at message
  54.         self.Close()
  55.     def OnMessButton(self, evt):
  56.         # event handler for the Message button click
  57.         self.SetStatusText('101 Different Ways to Spell "Spam"')
  58. class wxPyApp(wx.App):
  59.     def OnInit(self):
  60.         # set the title too
  61.         frame = Frame1(None, "wxPython GUI 2")
  62.         self.SetTopWindow(frame)
  63.         frame.Show(True)
  64.         return True
  65.        
  66. # get it going ...
  67. app = wxPyApp(redirect=True)
  68. app.MainLoop()
  69. #//python/5728

回复 "通过wxpython编写windows GUI程序"

这儿你可以回复上面这条便签

captcha