Python也跟大家一起分享了一段时间了,就像开始学C++一样,天天看着黑乎乎的控制台。
所以,今天就开始弄一弄Python中的GUI。
首先需要说明的是,Python没有一个标准的GUI工具包。所以也就是有不少的Python GUI包。
我们今天介绍的就是一个跨平台的Python GUI开发包 wxpython。
何为wxpython?
wxPython is a GUI toolkit for the Python programming language. It allows Python programmers to create programs with a robust, highly functional graphical user interface, simply and easily. It is implemented as a Python extension module (native code) that wraps the popular wxWidgets cross platform GUI library, which is written in C++.
Like Python and wxWidgets, wxPython is Open Source which means that it is free for anyone to use and the source code is available for anyone to look at and modify. Or anyone can contribute fixes or enhancements to the project.
wxPython is a cross-platform toolkit. This means that the same program will run on multiple platforms without modification. Currently supported platforms are 32-bit Microsoft Windows, most Unix or unix-like systems, and Macintosh OS X.
Since the language is Python, wxPython programs are simple, easy to write and easy to understand. Here is an example.
1明确自己的Python版本
例如2.7
2下载wxpyton,并傻瓜安装
http://www.wxpython.org/download.php
这里需要注意的是,不仅仅要知道自己的Python版本,还是清楚Python是32位还是64位。
大家不要有误区就行,只要知道64位系统上既可以安装32位的程序也可以安装64位的程序。
这么多废话的意思就是不要弄错了,python2.7为32位,就要下载:
wxPython3.0-win32-py27 32-bit Python 2.7
下载完成就是下一步 下一步 直到完成了。
3第一个GUI应用程序
我们用JetBrains PyCharm 3.4.1编写代码:
import wx
app = wx.App()
win = wx.Frame(None)
btn = wx.Button(win)
win.Show()
app.MainLoop()
加点酌料
import wx
app = wx.App()
win = wx.Frame(None, title = 'Simple Editor', size = (410, 335))
win.Show()
loadButton = wx.Button(win, label = 'Open', pos = (235, 5), size = (80, 25))
saveButton = wx.Button(win, label = 'Save', pos = (315, 5), size = (80, 25))
filename = wx.TextCtrl(win, pos=(5, 5), size = (210, 25))
#contents = wx.TextCtrl(win, pos=(5, 35), size = (390, 260), style=wx.TE_MULTILINE | wx.HScrolledWindow)
app.MainLoop()