简体   繁体   English

编译后重新生成 main class 导致重复

[英]Regenerating main class cause duplicating after compiling

I have GUI application with PyQt6 framework, and I need to change every single string without writing hundreds lines for every class.我有带有PyQt6框架的 GUI 应用程序,我需要更改每个字符串,而无需为每个 class 编写数百行。

I found the solution: just regenerate the instance of parent class, and everything will change automatically.我找到了解决方案:只需重新生成父 class 的实例,一切都会自动改变。

In code editor everything works well: regeneration main class -> it runs again.在代码编辑器中一切正常:regeneration main class -> 它再次运行。

But after compiling the problem appears: regeneration main class -> two classes.但是编译后出现问题:重新生成main class -> 两个类。

I'm using Nuitka for compiling with this command (this error appears on linux and windows):我正在使用Nuitka通过此命令进行编译(此错误出现在 linux 和 Windows 上):

python -m nuitka --onefile --follow-imports --plugin-enable=pyqt6 --disable-console program.py

program.py :程序.py

import sys
import random
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel

class Window(QWidget):
    def __init__(self):
        super().__init__()
        b = QPushButton('Random')
        b.clicked.connect(self._new)  # noqa
        self.layout = QVBoxLayout()
        self.layout.addWidget(b)
        self.setLayout(self.layout)
        self.show()
    def _new(self):
        self.rand = Number(random.randint(0, 10))

class Number(QWidget):
    def __init__(self, n):
        super().__init__()
        self.n = QLabel(str(n))
        self.b = QPushButton('Regenerate')
        self.b.clicked.connect(self.regenerate)  # noqa
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.n)
        self.layout.addWidget(self.b)
        self.setLayout(self.layout)
        self.show()

    @staticmethod
    def regenerate():
        global window
###################
        window = Window()  # Bug appears here
###################


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    app.exec()

Moving to PySide6 fixed the problem移动到 PySide6 解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM