在PyQt中通过点击一个按钮显示另一个控件

1 投票
2 回答
12787 浏览
提问于 2025-04-17 04:42

我刚开始学习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_())

我想实现的逻辑是:

  • 第一个小部件 - 主要部分(全屏显示)
  • 其他小部件 - 可以通过点击第一个小部件中的按钮来显示

2 个回答

0

我这台机器上没有安装PyQt4,所以没法测试。不过,我可以告诉你你遇到的问题:

showButton.clicked.connect(FileExplor.show)

你没有引用下面创建的那个小部件对象,而是引用了类对象FileExplor。

    fileExplorer = FileExplorer()
    fileExplorer.showFullScreen()

    fileExplor = FileExplor()
    fileExplor.show()

你能试着把FileExplor作为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。

2

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

在你发布的代码中,把 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)

撰写回答