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

发布于 2024-12-11 05:13:54 字数 2329 浏览 0 评论 0原文

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

逻辑我最终想要做什么:

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

I'm new in PyQt and using PyQt4. Have two independent widgets. First of them showFullScreen() and second show(). I want after hiding second by hide() show it by clicking button on first. Tried something and googled - nothing.
Full code:

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

Logic what i want make in the end:

  • first widget - main block (fullscreen)
  • other widgets - can be show by clicking buttons in first

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅哥哥的热头脑 2024-12-18 05:13:54

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

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

It sounds like what you want is a modeless dialog.

In the code you posted, change the FileExplor class to a QDialog:

class FileExplor(QtGui.QDialog):

Then add a signal handler to the main FileExplorer class:

def handleShowDialog(self):
    if not hasattr(self, 'dialog'):
        self.dialog = FileExplor(self)
    self.dialog.show()

And finally connect the button to the handler:

showButton.clicked.connect(self.handleShowDialog)
两仪 2024-12-18 05:13:54

我这台机器上没有安装 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。

I don't have PyQt4 installed on this machine so i can't test this. But here is your problem:

showButton.clicked.connect(FileExplor.show)

You are not referencing the widget object created below, you are referencing the class object FileExplor.

    fileExplorer = FileExplorer()
    fileExplorer.showFullScreen()

    fileExplor = FileExplor()
    fileExplor.show()

Can you try making FileExplor as a argument of FileExplorer?
Also, try naming FileExplor something else, like DependentFileExplorer (learn about naming conventions here) and do this:

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

Now FileExplorer takes DependentFileExplorer as an argument.
You must create the DependentFileExplorer before FileExplorer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文