Issue
- Click on the main window (A) to open a new window (B).
- Save the value at B. The example code saved the name through fileopen.
- If you close B and open B again through a click, the previously saved value remains.
All I want is to click and reset all the values.
Also, even if A is shut down while B is open, B remains.
I also want to know how to solve this problem.
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_())
Solution
The logic is really simple: create a method that sets the default values and then call that before displaying the window:
class CombineClass(QDialog):
# ...
def reset(self):
self.label2.clear()
# ...
class MyApp(QWidget):
# ...
def combine(self):
self.combine.reset()
self.combine.show()
# ...
Another possible solution is to create the dialog whenever it is required:
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())
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.