Qt与python的Wx图形界面

时间:2015-11-24 13:58:01

标签: python qt wxwidgets

到目前为止,我已使用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设计器创建的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 个答案:

答案 0 :(得分: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。然后我设置pushButton clicked信号以从该子类中触发printit函数:

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

...

def printit(self):
    print "qt print"

现在,按下按钮后,您将获得" qt print"对于每一个新闻界。