0

So I'm working on getting a popup working in multiple imported files, and come upon many answers and they all have the same format(see: Displaying pop-up windows in Python (PyQt4) )

They use this(summary):

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_())

This is crucial because to open the popup you need to keep a reference to it, like myapp is doing here.

In QTDesigner, it sets it up something like this in the if (__name__ == '__main__')

app = QtGui.QApplication(sys.argv)
m = Ui_Frame()
Frame = QtGui.QWidget()
m.setupUi(Frame)
Frame.show()

With the actual class/ui setup like this:

class Ui_Frame(object):

    def setupUi(self, Frame):

I tried to merge the two, but I am just getting a blank popup window like the setupUi isn't running. My code:

Edit: The new code with the disappearing window problem:

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 is just a simple:

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

I'm obviously missing something simple. setupUi is actually running, as everything in it is printing and such, but just nothing is showing up in the main window, just a blank frame

Community
  • 1
  • 1
user1610719
  • 1,275
  • 2
  • 18
  • 35

1 Answers1

1

Your Ui_Frame is the widget, so you don't need a separate 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)
        ...
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • Great start for me, much appreciated! Now I have this problem: I import this file from another, and pass some variables to it on the show() definition: `def Start(xlist, y): app = QtGui.QApplication(sys.argv) myapp = Ui_Frame() myapp.show() sys.exit(app.exec_())` So how can I now access this variable in the setupUi function? Aka, how do I pass it from the __init__ since I can't pass any variables to Ui_Frame(or at least don't know how) – user1610719 Nov 29 '12 at 09:45
  • 1
    @user1610719 you pass arguments to the constructor of `Ui_Frame`. Read http://docs.python.org/2/tutorial/classes.html#class-objects – ecatmur Nov 29 '12 at 09:58
  • Got it -- well now the problem is the frame isn't even showing. It is popping up and disappearing, and all the functions are showing up perfectly fine and printing correctly, but no frame. Just leaving `myapp = Ui_Frame(xlist, y) myapp.show()` in the function I'm calling, and it is calling everything but now no frame at all(or something is briefly popping up and disappearing). I've read this is because you aren't storing the myapp part. I am, but I am also calling the function that contains the start code and myapp variable from another module. – user1610719 Nov 29 '12 at 10:14
  • The method I'm using is as this link says should work, to see a better example(Except now I've got variables passed in the myapp=Ui_Frame() part and the __init__ function is accepting those): http://www.riverbankcomputing.com/pipermail/pyqt/2009-March/022171.html -- if I keep the `app = QtGui.QApplication(sys.argv)` parts I do get the error talked about there, but if I take it away and keep the code I said I'm still getting the disappearing frame – user1610719 Nov 29 '12 at 10:20
  • 1
    @user1610719 you need to keep a reference to `myapp` e.g. by returning it from `setup`. – ecatmur Nov 29 '12 at 11:23
  • It seems my main problem was not declaring myapp at the beginning as a global and then using global in the startup(). It works well now! – user1610719 Nov 29 '12 at 11:42