I've just realized only after having lost all the changes I made, that once you go back to the QT designer and amend something on the GUI and convert that back to a .py file all changes are erased on the previous .py file.
Would you recommend that I save it to a new .py file then copy & paste or is there a better 'professional' way to do this?
EDIT:
I have now managed to import the file instead of amending it with the code below:
from PyQt4 import QtCore, QtGui
from mysqlviewer import Ui_MainWindow
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I am now trying to simply add an item to the comboBox of the UI but it does not show.
Here is what I am attempting,
from PyQt4 import QtCore, QtGui
from mysqlviewer import Ui_MainWindow
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.comboBox_2.addItem("Example Item")
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Don't modify your auto-generated file. Just import and inherit from it:
from PySide import QtCore, QtGui
# modify the import if needed
# from PyQt5 import QtCore, QtGui
# from PyQt4 import QtCore, QtGui
from ui import Ui_MainWindow
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
"""Main Window.
"""
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
# your code here
Here ui
is the auto-generated module. No code changes are needed next time you update it.
I have found that the best way to import & use the file is as follows, this may just be in addition to the other answer.
import sys
from PyQt4 import QtCore, QtGui
from mysqlviewer import*
class Window(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.comboBox.addItem("Example Item")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
viewer = Window()
viewer.show()
sys.exit(app.exec_())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.