[英]python how to amend file after importing
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. 我只是在丢失所做的所有更改之后才意识到,一旦您回到QT设计器并在GUI上进行了一些修改并将其转换回.py文件,所有更改都将在先前的.py文件中删除。
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? 您是否建议我将其保存到新的.py文件中,然后进行复制和粘贴,或者是否有更好的“专业”方法来做到这一点?
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. 我现在正尝试将一个项目简单地添加到UI的comboBox中,但不显示。
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. ui
是自动生成的模块。 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_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.