【问题标题】:How can I initialize the new pyqt window?如何初始化新的 pyqt 窗口?
【发布时间】:2020-02-29 10:37:06
【问题描述】:
  1. 单击主窗口 (A) 打开一个新窗口 (B)。
  2. 将值保存在B。示例代码通过fileopen保存名称。
  3. 如果您通过单击关闭 B 并再次打开 B,则之前保存的值仍然存在。

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

此外,即使 A 在 B 打开时关闭,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_())

【问题讨论】:

  • 对不起,我第一次使用这个网站。请为每个句子插入一个标签。
  • import * 通常是不好的做法。

标签: python pyqt pyqt5


【解决方案1】:

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

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())

【讨论】:

  • 谢谢。但是当标签比现在多时,这种方法就不好了。如果我有 100 个标签,那么我会写 100 行 clear() 行。
  • @rut QLabels 名称是否有任何模式?你想清理所有的 QLabels 吗?
  • QLabel 只是示例。在我的真实代码中,有很多标签、按钮、框。而且它们没有模式......我想找到更方便的功能。
  • 但是这段代码还有一个问题。如果我在主窗口中打开一个窗口,之后主窗口将无法进行任何操作。因此,如果我无法在主窗口中使用另一个按钮打开另一个窗口。
  • 这很好用。但是,如果我使用这种方法,第一个问题仍然存在。关闭主窗口时存在问题,但新窗口仍然存在。没关系,但是你知道怎么解决吗?
猜你喜欢
  • 2016-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-21
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多