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

标签 python pyqt qwidget

我是 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_())

逻辑我最终想要做什么:

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

最佳答案

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

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

关于python - PyQt 通过单击另一个小部件上的按钮来显示一个小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7839600/

相关文章:

c++ - Qt5 和 Cmake 链接 QApplication 头错误

python - 如何在递归函数 Python 中使用列表?

Python lambda函数根据字典对列表进行排序

python - 按行比较列以进行部分字符串匹配

python - Tensorflow GPU内存报错try-except not catching the error

python - 尝试使用 QT 将 .ui 文件转换为 .py 文件,出现错误 ImportError : DLL load failed: %1 is not a valid Win32 application

python-2.7 - 如何在 PyQt4 中更改 QTextEdit 中所有内容的字体大小?

python - 从 QT Creator 转换对话框并将其合并到我的主窗口中的更好方法

c++ - 如何确定 QPushButton 的释放信号是自动重复还是实际鼠标释放的结果

qt - 在 Qt 中关闭新的非子窗口