Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于Pyside的查询_Python_Pyside - Fatal编程技术网

Python 关于Pyside的查询

Python 关于Pyside的查询,python,pyside,Python,Pyside,在下面提到的示例中,当我多次单击“查看”菜单下的“帮助”子菜单时,会创建多个窗口。谁能告诉我如何解决这个问题 import sys from PySide import Qt Gui from PySide.QtCore import Qt class Window(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) self.menu_bar()

在下面提到的示例中,当我多次单击“查看”菜单下的“帮助”子菜单时,会创建多个窗口。谁能告诉我如何解决这个问题

import sys
from PySide import Qt Gui
from PySide.QtCore import Qt


class Window(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.menu_bar()

    def menu_bar(self):
        helpAction = QtGui.QAction('&Help', self)
        helpAction.setShortcut('Ctrl+H')
        helpAction.triggered.connect(self.add_helpWindow)
        menu = self.menuBar().addMenu('View')
        menu.addAction(helpAction)

    def add_helpWindow(self):
        window = QtGui.QMainWindow(self)
        window.setWindowTitle('New Window')
        window.show()

if __name__ == '__main__':

    import sys
    app=QtGui.QApplication.instance()      
    if not app:
        app = QtGui.QApplication(sys.argv)
    window = Window()
    window.resize(300, 300)
    window.show()
    sys.exit(app.exec_())

您的帮助窗口只是一个
QMainWindow
,它不是模态窗口,并且对可能存在的数量没有限制。因此,如果多次选择“帮助”选项,则会出现多个窗口

您可能希望使用具有其
modal
属性集的。虽然没有任何东西强迫一次只存在一个对话框,但处于模态意味着只要该窗口打开,用户就只能与该窗口交互。例如:

from Pyside.QtGui import QMessageBox

def add_helpWindow(self):
    help_dialog = QMessageBox.information(self, 'Help', 'Some Help Text Here')
    help_dialog.setModal(True)
    return help_dialog.exec_()
您还可以使用获得更通用的对话框,它是
QMessageBox
的父类

如果这不是您想要的行为,您需要手动跟踪用户之前是否打开过该窗口,然后将用户关闭帮助窗口时发出的信号连接到重置存在跟踪器的插槽。下面是一个使用非模态对话框的示例:

from Pyside.QtGui import QDialog    

class Window(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.menu_bar()

        self.help_open = False # Tracks if the help dialog is already open

    def help_closed(self):
        self.help_open = False

    ...

    def add_helpWindow(self):
        if not self.help_open:
            self.help_open = True
            help_dialog = QDialog(self)
            # Any other setup code here
            help_dialog.setModal(False)
            help_dialog.accepted.connect(self.help_closed)
            help_dialog.rejected.connect(self.help_closed)
            help_dialog.show()

谢谢你的帮助reply@user3164132这能解决你的问题吗?如果是这样,请接受答案(单击答案旁边的复选框),以便将来的读者知道它是有效的。如果没有,请解释为什么它不起作用。