我只想单击并重置所有值。
此外,即使在打开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_())
逻辑非常简单:创建一个设置默认值的方法,然后在显示窗口之前调用它:
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())