wxPython安装方法

[复制链接]
1174|3
 楼主| gaoyang9992006 发表于 2018-8-28 11:23 | 显示全部楼层 |阅读模式
前提是先安装好PIP,不会的在本版块找我发的教程
运行 CMD 然后以管理员身份启动命令行
329425b84c02342f17.png
如上图所示,然后回车执行远程下载安装,最后成功安装。
 楼主| gaoyang9992006 发表于 2018-8-28 11:28 | 显示全部楼层
Hello world
安装好后我们测试一下怎么样。
建立一个Py文件命名为Hello.py
837405b84c133205af.png

保存。
双击运行
127225b84c14f0b2de.png
第一个例子成功,不过太简单了,接着我们复杂点,显示内容

 楼主| gaoyang9992006 发表于 2018-8-28 11:29 | 显示全部楼层
新建Hello2.py
  1. #!/bin/python
  2. """
  3. Hello World, but with more meat.
  4. """

  5. import wx

  6. class HelloFrame(wx.Frame):
  7.     """
  8.     A Frame that says Hello World
  9.     """

  10.     def __init__(self, *args, **kw):
  11.         # ensure the parent's __init__ is called
  12.         super(HelloFrame, self).__init__(*args, **kw)

  13.         # create a panel in the frame
  14.         pnl = wx.Panel(self)

  15.         # and put some text with a larger bold font on it
  16.         st = wx.StaticText(pnl, label="Hello World!", pos=(25,25))
  17.         font = st.GetFont()
  18.         font.PointSize += 10
  19.         font = font.Bold()
  20.         st.SetFont(font)

  21.         # create a menu bar
  22.         self.makeMenuBar()

  23.         # and a status bar
  24.         self.CreateStatusBar()
  25.         self.SetStatusText("Welcome to wxPython!")


  26.     def makeMenuBar(self):
  27.         """
  28.         A menu bar is composed of menus, which are composed of menu items.
  29.         This method builds a set of menus and binds handlers to be called
  30.         when the menu item is selected.
  31.         """

  32.         # Make a file menu with Hello and Exit items
  33.         fileMenu = wx.Menu()
  34.         # The "\t..." syntax defines an accelerator key that also triggers
  35.         # the same event
  36.         helloItem = fileMenu.Append(-1, "&Hello...\tCtrl-H",
  37.                 "Help string shown in status bar for this menu item")
  38.         fileMenu.AppendSeparator()
  39.         # When using a stock ID we don't need to specify the menu item's
  40.         # label
  41.         exitItem = fileMenu.Append(wx.ID_EXIT)

  42.         # Now a help menu for the about item
  43.         helpMenu = wx.Menu()
  44.         aboutItem = helpMenu.Append(wx.ID_ABOUT)

  45.         # Make the menu bar and add the two menus to it. The '&' defines
  46.         # that the next letter is the "mnemonic" for the menu item. On the
  47.         # platforms that support it those letters are underlined and can be
  48.         # triggered from the keyboard.
  49.         menuBar = wx.MenuBar()
  50.         menuBar.Append(fileMenu, "&File")
  51.         menuBar.Append(helpMenu, "&Help")

  52.         # Give the menu bar to the frame
  53.         self.SetMenuBar(menuBar)

  54.         # Finally, associate a handler function with the EVT_MENU event for
  55.         # each of the menu items. That means that when that menu item is
  56.         # activated then the associated handler function will be called.
  57.         self.Bind(wx.EVT_MENU, self.OnHello, helloItem)
  58.         self.Bind(wx.EVT_MENU, self.OnExit,  exitItem)
  59.         self.Bind(wx.EVT_MENU, self.OnAbout, aboutItem)


  60.     def OnExit(self, event):
  61.         """Close the frame, terminating the application."""
  62.         self.Close(True)


  63.     def OnHello(self, event):
  64.         """Say hello to the user."""
  65.         wx.MessageBox("Hello again from wxPython")


  66.     def OnAbout(self, event):
  67.         """Display an About Dialog"""
  68.         wx.MessageBox("This is a wxPython Hello World sample",
  69.                       "About Hello World 2",
  70.                       wx.OK|wx.ICON_INFORMATION)


  71. if __name__ == '__main__':
  72.     # When this module is run (not imported) then create the app, the
  73.     # frame, show it, and start the event loop.
  74.     app = wx.App()
  75.     frm = HelloFrame(None, title='Hello World 2')
  76.     frm.Show()
  77.     app.MainLoop()
 楼主| gaoyang9992006 发表于 2018-8-28 11:32 | 显示全部楼层
355995b84c246a101f.png
这次发行双击不行了,需要以Python Shell方式运行。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:如果你觉得我的分享或者答复还可以,请给我点赞,谢谢。

2052

主题

16403

帖子

222

粉丝
快速回复 在线客服 返回列表 返回顶部