0

所以我正在努力让一个弹出窗口在多个导入的文件中工作,并得到许多答案,它们都有相同的格式(参见: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()

使用像这样的实际类/用户界面设置:

class Ui_Frame(object):

    def setupUi(self, Frame):

我试图合并这两者,但我只是得到一个空白的弹出窗口,就像 setupUi 没有运行一样。我的代码:

编辑:具有消失窗口问题的新代码:

导入文件.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 实际上正在运行,因为其中的所有内容都在打印等,但主窗口中没有显示任何内容,只是一个空白框架

4

1 回答 1

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)
        ...
于 2012-11-29T08:50:13.797 回答