wxPython和PyQt分别是wxWidgets和Qt的python绑定,wxWidgets和Qt都是跨平台的GUI库,不过前者是开源免费的,而后者是基于商业License
让我们分别看看wxPython和PyQt的Hello World程序
wxPython
首先去
http://www.python.org下载Windows下的python2.5,然后去
http://www.wxpython.org下载相应的Windows安装包
装好后写个hellowx.py看看效果:
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title, pos=(150, 150), size=(350, 200))
menuBar = wx.MenuBar()
self.SetMenuBar(menuBar)
menu = wx.Menu()
menu.Append(wx.ID_EXIT, "E&xit", "Exit this application")
self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)
menuBar.Append(menu, "&File")
panel = wx.Panel(self)
text = wx.StaticText(panel, -1, "Hello wxPython!")
text.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
text.SetSize(text.GetBestSize())
panel.Layout()
def OnTimeToClose(self, evt):
self.Close()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, "Hello wxPython!")
self.SetTopWindow(frame)
frame.Show(True)
return True
app = MyApp()
app.MainLoop()
App->Frame->MenuBar/Panel,结构很清晰
PyQt
去
http://www.quadgames.com/download/pythonqt/下载Windows的PyQt安装包,PyQtGPL10.exe目前只支持到Python2.4
然后写个helloqt.py看看效果:
import sys
from qt import *
class HelloButton(QPushButton):
def __init__(self, *args):
QPushButton.__init__(self, *args)
self.setText("Hello World")
class HelloWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.button = HelloButton(self)
self.setCentralWidget(self.button)
def main(args):
app = QApplication(args)
win = HelloWindow()
win.show()
app.connect(app, SIGNAL("lastWindowClosed()"),
app, SLOT("quit()"))
app.exec_loop()
if __name__ == "__main__":
main(sys.argv)
也是App->Window->MenuBar的模式,同wx没多大区别
体验
总体觉得Qt的类名起的有点怪异,wxPython看起来很优美