欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wxPython和PyQt的Hello World例子比较

程序员文章站 2022-06-01 17:58:58
...
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看起来很优美

上一篇: Qt状态机实例

下一篇: QOdbc读写excel