Denis DEMIR
Denis DEMIR

Reputation: 1

open window multiple times in loop

I hope someone can help me...

The Goal: When the button "button_trog" is clicked it should start a loop that opens up a new window a specific amount of time based on the number chosen from the spinbox "spinbox_count" giving the possiblity to enter something.

Situation: That works so far but the problem is that the window only opens up one time.

It should open up again when I click the accepted button.

I am not sure what to try next, because for my understanding it should open up the window again... I mean the loop is working, I tried that with the print() function.

This is the code from the main window file:

CODE: spinbox_count

self.spinbox_count = QtWidgets.QSpinBox(self.centralwidget)
    self.spinbox_count.setGeometry(QtCore.QRect(370, 430, 70, 30))
    font = QtGui.QFont()
    font.setPointSize(15)
    self.spinbox_count.setFont(font)
    self.spinbox_count.setObjectName("spinbox_count")

CODE: button_trog

self.button_trog = QtWidgets.QPushButton(self.centralwidget)
    self.button_trog.setGeometry(QtCore.QRect(530, 390, 200, 100))
    font = QtGui.QFont()
    font.setPointSize(15)
    self.button_trog.setFont(font)
    self.button_trog.setObjectName("button_trog")
    self.button_trog.clicked.connect(self.scanning)

CODE: def scanning

    def scanning(self):
    count = self.spinbox_count.value()
    self.lcount = 0

    if count < 1:
        print("Bitte geben Sie eine Stückzahl ein!")

    while self.lcount < count:
        self.window = QtWidgets.QDialog()
        self.ui = Ui_window_scanning()
        self.ui.setupUi(self.window)
        self.window.show()
        self.lcount = self.lcount + 1

Here starts the code from the second file for the "pop up" dialog window.

CODE: window dialog

class Ui_window_scanning(object):
def setupUi(self, window_scanning):
    window_scanning.setObjectName("window_scanning")
    window_scanning.resize(400, 300)
    window_scanning.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)

    # Label Description Scanning
    self.label_scanning = QtWidgets.QLabel(window_scanning)
    self.label_scanning.setGeometry(QtCore.QRect(30, 20, 341, 71))
    font = QtGui.QFont()
    font.setPointSize(15)
    self.label_scanning.setFont(font)
    self.label_scanning.setAlignment(QtCore.Qt.AlignCenter)
    self.label_scanning.setObjectName("label_scanning")

    # Input Barcode
    self.lineedit_scanning = QtWidgets.QLineEdit(window_scanning)
    self.lineedit_scanning.setGeometry(QtCore.QRect(100, 120, 200, 30))
    font = QtGui.QFont()
    font.setPointSize(12)
    self.lineedit_scanning.setFont(font)
    self.lineedit_scanning.setObjectName("lineedit_scanning")

    # Button Accept and Cancel
    self.buttonbox_scanning = QtWidgets.QDialogButtonBox(window_scanning)
    self.buttonbox_scanning.setGeometry(QtCore.QRect(120, 210, 156, 23))
    self.buttonbox_scanning.setStandardButtons(
        QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok)
    self.buttonbox_scanning.setObjectName("buttonbox_scanning")
    self.buttonbox_scanning.rejected.connect(window_scanning.reject)
    self.buttonbox_scanning.accepted.connect(window_scanning.accept)

Thanks in advance!

Upvotes: 0

Views: 143

Answers (1)

musicamante
musicamante

Reputation: 48260

The problem is that the self.window attribute is overwritten everytime the while loops, and since the dialog has no other reference, it gets garbage collected (aka, deleted). You don't see only one window, you see the last window.

There are two similar solutions:

  • add the windows to a persistent list, so that they cannot be deleted;
  • set a parent to the windows;

If you need to keep track of the newly created windows (because you want to close them programmatically, or need to access their contents), a list is required anyway.

def scanning(self):
        # ...
        self.window = QtWidgets.QDialog(self)

Note, and this is very important: the modification above is based on the assumption that you're correctly using a subclass of QWidget (such as QMainWindow), and you're not using a modified file generated by the pyuic tool (or trying to mimic its behavior) and implementing scanning in any of its created classes (normally named Ui_MainWindow or similar and based on the simple python object).

If you're doing this (as I'm afraid to), consider that:

  1. it won't work, as self is not a subclass of QWidget, which is required as a parent argument for any QWidget instance;
  2. it's considered bad practice (there are lots of reasons for which it should never be done, one of which is confusion or problems with the object structure, like in this case);

In this case, read more about this topic on the official guidelines about using Designer, generate again the code with pyuic and implement the logic of your program (with the above functions) as explained in that documentation - the multiple-inheritance approach is normally the better.
Please consider that any attempt to continue to use modified pyuic files due to laziness ("I've already written a lot of code!") or lack of understanding of the proper usage of Qt subclasses will only lead to a continously increasing amount of problems and issues that, at some point, will force you to switch to the common approach anyway - but at that point it will be much more painful and difficult.

Upvotes: 1

Related Questions