【问题标题】:Qt vs. Wx graphical interface for pythonQt 与 Wx 的 python 图形界面
【发布时间】:2016-02-27 00:10:35
【问题描述】:

到目前为止,我已经使用wx 为 python 创建图形界面。我尝试对Qt 做同样的事情。这是问题所在。在wx 中,可以保持由设计器软件(即wxformbuilder)创建的.py 文件不变,只需导入其类并添加额外的事件函数。因为wx 在主GUI.py 文件中创建了空事件函数,并自行完成所有连接。

我在下面输入一个由 wxformbuilder 创建的简单wxexample。请注意# 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 以使用gui 代码qt_gui.py 而不触及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_())

【问题讨论】:

    标签: python qt wxwidgets


    【解决方案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”。

    【讨论】:

      猜你喜欢
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      相关资源
      最近更新 更多