wxPython和PyQt的Hello World例子比较

wxPython和PyQt分别是wxWidgets和Qt的python绑定,wxWidgets和Qt都是跨平台的GUI库,不过前者是开源免费的,而后者是基于商业License
让我们分别看看wxPython和PyQt的Hello World程序

[b]wxPython[/b]
首先去[url]http://www.python.org[/url]下载Windows下的python2.5,然后去[url]http://www.wxpython.org[/url]下载相应的Windows安装包
装好后写个hellowx.py看看效果:
[code]
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()
[/code]
App->Frame->MenuBar/Panel,结构很清晰

[b]PyQt[/b]
去[url]http://www.quadgames.com/download/pythonqt/[/url]下载Windows的PyQt安装包,PyQtGPL10.exe目前只支持到Python2.4
然后写个helloqt.py看看效果:
[code]
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)
[/code]
也是App->Window->MenuBar的模式,同wx没多大区别

[b]体验[/b]
总体觉得Qt的类名起的有点怪异,wxPython看起来很优美

你可能感兴趣的:(Python)