Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 PyQt通过单击另一个小部件上的按钮来显示一个小部件_Python_Pyqt_Qwidget - Fatal编程技术网

Python PyQt通过单击另一个小部件上的按钮来显示一个小部件

Python PyQt通过单击另一个小部件上的按钮来显示一个小部件,python,pyqt,qwidget,Python,Pyqt,Qwidget,我是PyQt新手,使用PyQt4。有两个独立的小部件。第一个是showFullScreen(),第二个是show()。我想在通过hide()隐藏第二个后单击第一个按钮显示它。尝试了一些东西,然后谷歌搜索——什么也没有。 完整代码: from PyQt4 import QtCore, QtGui class FileExplorer(QtGui.QWidget): def __init__(self, parent=None): super(FileExplorer,

我是PyQt新手,使用PyQt4。有两个独立的小部件。第一个是
showFullScreen()
,第二个是
show()
。我想在通过
hide()隐藏第二个后单击第一个按钮显示它。尝试了一些东西,然后谷歌搜索——什么也没有。
完整代码:

from PyQt4 import QtCore, QtGui


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

        nameLabel = QtGui.QLabel("Name:")
        self.nameLine = QtGui.QLineEdit()

        addressLabel = QtGui.QLabel("Address:")
        self.addressText = QtGui.QTextEdit()

        showButton = QtGui.QPushButton('Show widget', self)
        showButton.clicked.connect(FileExplor.show)

        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(nameLabel, 0, 0)
        mainLayout.addWidget(self.nameLine, 0, 1)
        mainLayout.addWidget(addressLabel, 1, 0, QtCore.Qt.AlignTop)
        mainLayout.addWidget(self.addressText, 1, 1)
        mainLayout.addWidget(showButton, 3, 1)

        self.setLayout(mainLayout)
#               self.setGeometry(300, 300, 250, 150)
#        self.sizeHint()
        self.setWindowTitle("File Explorer")




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

        nameLabel = QtGui.QLabel("Name:")
        self.nameLine = QtGui.QLineEdit()

        addressLabel = QtGui.QLabel("Address:")
        self.addressText = QtGui.QTextEdit()

        quitButton = QtGui.QPushButton('Quit', self)
        quitButton.clicked.connect(self.hide)

        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(nameLabel, 0, 0)
        mainLayout.addWidget(self.nameLine, 0, 1)
        mainLayout.addWidget(addressLabel, 1, 0, QtCore.Qt.AlignTop)
        mainLayout.addWidget(self.addressText, 1, 1)
        mainLayout.addWidget(quitButton, 3, 1)

        self.setLayout(mainLayout)
        #self.setGeometry(300, 300, 250, 150)
        self.sizeHint()
        self.setWindowTitle("File Explorer")


if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)

    fileExplorer = FileExplorer()
    fileExplorer.showFullScreen()
#    fileExplorer.show()
#
    fileExplor = FileExplor()
    fileExplor.show()

    sys.exit(app.exec_())
逻辑我到底想做什么:

  • 第一个小部件-主块(全屏)
  • 其他小部件-可以通过单击第一个窗口中的按钮来显示

我没有在这台机器上安装PyQt4,因此无法测试它。但这是你的问题:

showButton.clicked.connect(FileExplor.show)
您不是在引用下面创建的小部件对象,而是在引用类对象FileExplorer

    fileExplorer = FileExplorer()
    fileExplorer.showFullScreen()

    fileExplor = FileExplor()
    fileExplor.show()
能否尝试将
FileExplorer
作为
FileExplorer
的参数? 另外,尝试命名
FileExplor
其他名称,如
DependentFileExplorer
(了解命名约定)并执行以下操作:

from PyQt4 import QtCore, QtGui


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

        nameLabel = QtGui.QLabel("Name:")
        self.nameLine = QtGui.QLineEdit()

        addressLabel = QtGui.QLabel("Address:")
        self.addressText = QtGui.QTextEdit()

        showButton = QtGui.QPushButton('Show widget', self)
        showButton.clicked.connect(self.dependent.show)

        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(nameLabel, 0, 0)
        mainLayout.addWidget(self.nameLine, 0, 1)
        mainLayout.addWidget(addressLabel, 1, 0, QtCore.Qt.AlignTop)
        mainLayout.addWidget(self.addressText, 1, 1)
        mainLayout.addWidget(showButton, 3, 1)

        self.setLayout(mainLayout)
        self.setWindowTitle("File Explorer")




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

        nameLabel = QtGui.QLabel("Name:")
        self.nameLine = QtGui.QLineEdit()

        addressLabel = QtGui.QLabel("Address:")
        self.addressText = QtGui.QTextEdit()

        quitButton = QtGui.QPushButton('Quit', self)
        quitButton.clicked.connect(self.hide)

        mainLayout = QtGui.QGridLayout()
        mainLayout.addWidget(nameLabel, 0, 0)
        mainLayout.addWidget(self.nameLine, 0, 1)
        mainLayout.addWidget(addressLabel, 1, 0, QtCore.Qt.AlignTop)
        mainLayout.addWidget(self.addressText, 1, 1)
        mainLayout.addWidget(quitButton, 3, 1)

        self.setLayout(mainLayout)
        #self.setGeometry(300, 300, 250, 150)
        self.sizeHint()
        self.setWindowTitle("File Explorer")


if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)

    dependent = DependentFileExplorer()
    fileExplorer = FileExplorer(dependent)

    fileExplorer.showFullScreen()
    dependent.show()

    sys.exit(app.exec_())
现在FileExplorer将DependentFileExplorer作为参数

必须在FileExplorer之前创建DependentFileExplorer

听起来您想要的是一个无模式对话框

在您发布的代码中,将
FileExplor
类更改为
QDialog

class FileExplor(QtGui.QDialog):
然后将信号处理程序添加到主
FileExplorer
类:

def handleShowDialog(self):
    if not hasattr(self, 'dialog'):
        self.dialog = FileExplor(self)
    self.dialog.show()
最后,将按钮连接到处理程序:

showButton.clicked.connect(self.handleShowDialog)