我如何初始化新的pyqt窗口?

问题描述 投票:0回答:1
  1. 单击主窗口(A)打开一个新窗口(B)。
  2. 将值保存在B。示例代码通过fileopen保存了名称。
  3. 如果关闭B并通过单击再次打开B,则保留以前保存的值。

我只想单击并重置所有值。

此外,即使在打开B的同时关闭A,B仍然存在。

我也想知道如何解决这个问题。

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class CombineClass(QDialog):
    def __init__(self, parent=None):
        super(CombineClass, self).__init__(parent)
        self.initUI()

    def initUI(self):
        self.setWindowFlag(Qt.WindowContextHelpButtonHint, False)


        self.label = QPushButton("file name", self)
        self.label.clicked.connect(self.fileselect)
        self.label.move(150,100)

        self.label2 = QLabel("print", self)
        self.label2.move(50,100)

        self.setWindowTitle("combine")
        self.resize(300, 200)
        self.center()
    def fileselect(self):

        filename = QFileDialog.getOpenFileNames(self, "Open Files", "C:\\Users\\", "(*.txt)")
        self.label2.setText(filename[0][0])

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

        self.combine = CombineClass()
    def initUI(self):
        self.fileselect = QPushButton("파일", self)
        self.fileselect.clicked.connect(self.combine)
        self.setWindowTitle("Text Master")
        self.resize(600, 600)
        self.center()
        self.show()
    def combine(self):
        self.combine.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())
python pyqt pyqt5
1个回答
0
投票

逻辑非常简单:创建一个设置默认值的方法,然后在显示窗口之前调用它:

class CombineClass(QDialog):
    # ...
    def reset(self):
        self.label2.clear()
    # ...

class MyApp(QWidget):
    # ...
    def combine(self):
        self.combine.reset()
        self.combine.show()
    # ...

另一种可能的解决方案是在需要时创建对话框:

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        # self.combine = CombineClass()

    def initUI(self):
        self.fileselect = QPushButton("파일", self)
        self.fileselect.clicked.connect(self.combine)
        self.setWindowTitle("Text Master")
        self.resize(600, 600)
        self.center()
        self.show()

    def combine(self):
        self.combine = CombineClass()
        self.combine.show()
        print("after close")

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())
© www.soinside.com 2019 - 2024. All rights reserved.