【问题标题】:open window multiple times in loop在循环中多次打开窗口
【发布时间】:2021-03-06 16:30:25
【问题描述】:

我希望有人可以帮助我...

目标: 当按钮“button_trog”被点击时,它应该开始一个循环,根据从旋转框“spinbox_count”中选择的数字打开一个新窗口的特定时间量,从而可以输入一些东西。

情况: 到目前为止有效,但问题是窗口只打开一次。

当我点击接受按钮时它应该会再次打开。

我不确定接下来要尝试什么,因为据我了解,它应该再次打开窗口... 我的意思是循环正在工作,我用 print() 函数尝试过。

这是来自主窗口文件的代码:

代码: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")

代码: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)

代码:def扫描

    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

这里从“弹出”对话框窗口的第二个文件开始代码。

代码:窗口对话框

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)

提前致谢!

【问题讨论】:

标签: python loops user-interface pyqt5 window


【解决方案1】:

问题在于 self.window 属性在每次 while 循环时都会被覆盖,并且由于对话框没有其他引用,因此它会被垃圾收集(也就是删除)。您不会只看到一个窗口,而是看到最后一个窗口。

有两种类似的解决方案:

  • 将窗口添加到永久列表中,使其无法被删除;
  • 为窗口设置父级;

如果您需要跟踪新创建的窗口(因为您想以编程方式关闭它们,或者需要访问它们的内容),无论如何都需要一个列表。

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

注意,这非常很重要:上面的修改是基于您正确使用 QWidget 的子类(例如 QMainWindow)的假设,而您 不是 使用由 pyuic 工具生成的修改文件(或试图模仿其行为)并在其创建的任何类中实现 scanning(通常命名为 Ui_MainWindow 或类似名称,基于简单的 python object) .

如果您正在这样做(我很害怕),请考虑:

  1. 它不起作用,因为self 不是 QWidget 的子类,任何 QWidget 实例都需要它作为父参数;
  2. 它被认为是不好的做法(有很多原因不应该这样做,其中之一是对象结构的混乱或问题,就像在这种情况下);

在这种情况下,请阅读有关using Designer 的官方指南中有关此主题的更多信息,使用 pyuic 再次生成代码并按照该文档中的说明实现程序的逻辑(使用上述函数) - 多重继承方法通常更好。
请考虑任何由于懒惰(“我已经写了很多代码!”)或对 Qt 子类的正确使用缺乏了解而继续使用修改过的 pyuic 文件的任何尝试,只会导致问题数量不断增加以及在某些时候会迫使您切换到通用方法的问题 - 但到那时会更加痛苦和困难。

【讨论】:

    猜你喜欢
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2023-04-04
    • 2017-12-10
    • 2021-11-17
    • 1970-01-01
    相关资源
    最近更新 更多