【问题标题】:PyQt : Prevent a window to be opened several timesPyQt:防止窗口多次打开
【发布时间】:2014-02-03 14:09:32
【问题描述】:

我以下面的简单代码为例。它只是打开一个新窗口点击一个按钮。如果它已经在屏幕上,我找不到阻止重新打开此小部件的方法。如果窗口已经存在,我想打开一个 QDialog 警告,主要是让 closeEvent 方法向 Mainwidget 发送一个信号,说明新窗口已关闭。这将允许再次打开 newWidget。

import sys
from PyQt4 import QtCore, QtGui

class NewWidget(QtGui.QWidget): 
    def __init__(self, parent=None):
        super(NewWidget,self).__init__(parent)

        self.lineEdit = QtGui.QLineEdit('new window',self)
        self.resize(200,50)
        self.show()

    def closeEvent(self,ev):

        self.Exit = QtGui.QMessageBox.question(self,
                  "Confirm Exit...",
                  "Are you sure you want to exit ?",
                  QtGui.QMessageBox.Yes| QtGui.QMessageBox.No)
        ev.ignore()

        if self.Exit  == QtGui.QMessageBox.Yes:            
            ev.accept()     

class MainWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MainWidget,self).__init__(parent)

        self.button = QtGui.QPushButton("button", self)
        self.button.clicked.connect(self.open_new)

    def open_new(self):

        self.new = NewWidget()

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    main = MainWidget()
    main.resize(200,50)
    main.move(app.desktop().screen().rect().center() - main.rect().center())
    main.show()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python events window pyqt4


    【解决方案1】:

    我认为更好的解决方案是避免每次单击按钮时都创建一个新窗口。

    一种方法是将子窗口更改为 QDialog:

    class NewWidget(QtGui.QDialog):
    ...
    

    并将调整大小/显示行移动到open_new 方法中:

    class MainWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            ...
            self._subwindow = None
    
        def open_new(self):
            if self._subwindow is None:
                self._subwindow = NewWidget(self)
                self._subwindow.resize(200, 50)
                # move it next to the main window
                pos = self.frameGeometry().topLeft()
                self._subwindow.move(pos.x() - 250, pos.y())
            self._subwindow.show()
            self._subwindow.activateWindow()
    

    所以现在只有一个子窗口,只要单击按钮就会重新激活。

    【讨论】:

      【解决方案2】:

      太好了。我的问题的最终解决方案如下所示:

      class MainWidget(QtGui.QWidget):
      def __init__(self, parent=None):
          ...
          self._subwindow = QtGui.Qdialog()
      
      def open_new(self):
          if self.subwindow.isVisible() is False:
              self._subwindow = NewWidget(self)
              self._subwindow.resize(200, 50)
              # move it next to the main window
              pos = self.frameGeometry().topLeft()
              self._subwindow.move(pos.x() - 250, pos.y())
          self._subwindow.show()
          self._subwindow.activateWindow()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-26
        • 1970-01-01
        • 1970-01-01
        • 2010-11-20
        相关资源
        最近更新 更多