所以我正在努力让弹出窗口工作在多个导入的文件中,并得到许多答案,它们都具有相同的格式(参见:Displaying pop-up windows in Python (PyQt4))
他们使用这个(摘要):
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
# Usual setup stuff
self.ui = Ui_Dialog()
self.ui.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp= MyForm()
myapp.show()
sys.exit(app.exec_())
这是至关重要的,因为打开弹出窗口需要保留对它的引用,就像myapp在这里做的那样。
在QTDesigner中,它在if (__name__ == '__main__')
app = QtGui.QApplication(sys.argv)
m = Ui_Frame()
Frame = QtGui.QWidget()
m.setupUi(Frame)
Frame.show()
使用这样的实际类/ ui设置:
class Ui_Frame(object):
def setupUi(self, Frame):
我试图将两者合并,但我只是得到一个空白的弹出窗口,就像setupUi没有运行一样。我的代码:
编辑:带有消失窗口问题的新代码:
importedfile.py
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_Frame(QtGui.QWidget):
def __init__(self, xlist, y, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setupUi(xlist,y)
def setupUi(self, xlist, y):
self.setObjectName(_fromUtf8("Frame"))
self.resize(800, 400)
self.tableWidget = QtGui.QTableWidget(self)
...
def startup(xlist, y):
myapp= Ui_Frame(xlist, y)
myapp.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp= Ui_Frame()
myapp.show()
sys.exit(app.exec_())
other.py只是一个简单的事情:
import importedfile
#app = QtGui.QApplication(sys.argv) and sys.exit are somewhere up here for the other frame
...code...
tempholder = importedfile.startup(xlist,y)
#Using a var for the startup() or not using has the same results
我显然错过了一些简单的事情。 setupUi实际上正在运行,因为其中的所有内容都是打印等等,但主窗口中没有显示任何内容,只是一个空白框架
答案 0 :(得分:1)
您的Ui_Frame
是小工具,因此您不需要单独的Frame
:
class Ui_Frame(QtGui.QWidget):
def __init__(self, parent=None):
# Here, you should call the inherited class' init, which is QDialog
QtGui.QWidget.__init__(self, parent)
xlist = [1,2,3]
y = "Default"
self.setupUi(xlist,y)
def setupUi(self, xlist, y):
self.setObjectName(_fromUtf8("Frame"))
self.resize(800, 400)
self.tableWidget = QtGui.QTableWidget(self)
...