六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 29|回复: 0

wxPython和PyQt的Hello World例子比较

[复制链接]

升级  84.33%

137

主题

137

主题

137

主题

举人

Rank: 3Rank: 3

积分
453
 楼主| 发表于 2013-1-28 13:15:20 | 显示全部楼层 |阅读模式
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 wxclass 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 sysfrom 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看起来很优美
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表