Qt与Wx图形界面在Python中的对比

0 投票
1 回答
596 浏览
提问于 2025-05-11 00:13

到目前为止,我一直在用 wx 来创建 Python 的图形界面。现在我想用 Qt 来做同样的事情。这里遇到的问题是,在 wx 中,我可以保持设计软件(比如 wxformbuilder)生成的 .py 文件不变,只需导入它的类,然后添加额外的事件函数。因为 wx 会在主 GUI.py 文件中自动创建空的事件函数,并自己处理所有的连接。

下面是一个简单的由 wxformbuilder 创建的 wx 示例。请注意 # Virtual event handlers。将下面的代码命名为 wx_gui.py

# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc

###########################################################################
## Class MyFrame1
###########################################################################

class MyFrame1 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_button1 = wx.Button( self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.m_button1, 0, wx.ALL, 5 )


        self.SetSizer( bSizer1 )
        self.Layout()

        self.Centre( wx.BOTH )

        # Connect Events
        self.m_button1.Bind( wx.EVT_BUTTON, self.onClick )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def onClick( self, event ):
        event.Skip()

然后我用一段单独的代码来使用上面的代码(命名为 wx_main.py):

import wx
import wx_gui

class MainApp (wx_gui.MyFrame1):
    def __init__( self, parent ):
        wx_gui.MyFrame1.__init__(self,parent)

    def onClick( self, event ):
        print "WX clicked"


app = wx.App(False)
#create an object of PytnerClass
frame = MainApp(None)
#show the frame
frame.Show(True)
#start the applications
app.MainLoop()

我想用 Qt Designer 创建的 GUI 代码做完全相同的事情。下面是 pyuic4 命令的原始输出(命名为 qt_gui.py):

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'qt_gui.ui'
#
# Created: Tue Nov 24 14:37:40 2015
#      by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.horizontalLayout_2 = QtGui.QHBoxLayout(Form)
        self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.horizontalLayout.addWidget(self.pushButton)
        self.horizontalLayout_2.addLayout(self.horizontalLayout)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "PushButton", None))

我该如何编写一个 qt_main.py 来使用 qt_gui.py 中的 GUI 代码,而不去修改 pyuic4 的原始输出,因为每次都会覆盖我的更改。

下面是如果我直接编辑 qt_gui.py 的代码(但我不想这样做):

from PyQt4 import QtCore, QtGui
import sys

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)

    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.horizontalLayout_2 = QtGui.QHBoxLayout(Form)
        self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.horizontalLayout.addWidget(self.pushButton)
        self.horizontalLayout_2.addLayout(self.horizontalLayout)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton.setText(_translate("Form", "PushButton", None))
        self.pushButton.clicked.connect(self.printit)

    def printit(self):
        print "qt print"

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = Ui_Form()
    ex.show()
    sys.exit(app.exec_())

相关文章:

  • 暂无相关问题
暂无标签

1 个回答

1

创建一个类,使用自动生成的 Ui_Form

from PyQt4 import QtCore, QtGui
from qt_gui import Ui_Form
import sys

class ExampleApp(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(ExampleApp, self).__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        # Set up your signals at this point:
        self.ui.pushButton.clicked.connect(self.printit)

    def printit(self):
        print "qt print"    


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = ExampleApp()
    ex.show()
    sys.exit(app.exec_())

在这个例子中,我创建了一个叫 ExampleApp 的类。然后我设置了 pushButtonclicked 信号,让它在被点击时执行 printit 这个函数:

self.ui.pushButton.clicked.connect(self.printit)

...

def printit(self):
    print "qt print"

现在,当你按下这个按钮时,每次都会显示 "qt print"。

撰写回答