python 的 Qt 与 Wx 图形界面
Qt vs. Wx graphical interface for python
到目前为止,我已经使用 wx
为 python 创建了图形界面。我尝试对 Qt
做同样的事情。这就是问题所在。在 wx
中,可以保持由设计器软件(即 wxformbuilder)创建的 .py
文件不变,只需导入其 class 并添加额外的事件函数。因为 wx
在 GUI.py
主文件中创建了空事件函数,并自行完成所有连接。
一个简单的wx
示例,由我在下面输入的 wxformbuilder 创建。请注意# 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_())
创建一个 class,利用自动生成的 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
信号以从该 subclass:
中触发 printit
函数
self.ui.pushButton.clicked.connect(self.printit)
...
def printit(self):
print "qt print"
现在,当您按下按钮时,每次按下都会得到 "qt print"。
到目前为止,我已经使用 wx
为 python 创建了图形界面。我尝试对 Qt
做同样的事情。这就是问题所在。在 wx
中,可以保持由设计器软件(即 wxformbuilder)创建的 .py
文件不变,只需导入其 class 并添加额外的事件函数。因为 wx
在 GUI.py
主文件中创建了空事件函数,并自行完成所有连接。
一个简单的wx
示例,由我在下面输入的 wxformbuilder 创建。请注意# 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_())
创建一个 class,利用自动生成的 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
信号以从该 subclass:
printit
函数
self.ui.pushButton.clicked.connect(self.printit)
...
def printit(self):
print "qt print"
现在,当您按下按钮时,每次按下都会得到 "qt print"。