До сих пор я использовал wx
для создания графических интерфейсов для python. Я пытаюсь сделать то же самое с Qt
. Вот проблема. В файле wx
можно сохранить файл .py
, созданный программным обеспечением дизайнера (т. Е. Wxformbuilder), нетронутым и просто импортировать его класс и добавить дополнительные функции событий. Поскольку wx
создает пустую функцию события в главном файле GUI.py
и выполняет все соединения сам по себе.Qt vs. Wx графический интерфейс для python
Простой пример 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()
Я хочу сделать то же самое, используя код GUI, созданный Qt дизайнер. Вот оригинальный выход 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
, не касаясь оригинальным выходом 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_())