我是PyQt的新手并使用PyQt4。有两个独立的小部件。其中第一个showFullScreen()
和第二个show()
。我希望在hide()
之后隐藏第二个,先点击按钮显示它。尝试了一些东西并用Google搜索 - 没什么。
完整代码:
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_())
最终我想要的逻辑:
答案 0 :(得分: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)
答案 1 :(得分:0)
我没有在这台机器上安装PyQt4,所以我无法测试这个。但这是你的问题:
showButton.clicked.connect(FileExplor.show)
您没有引用下面创建的窗口小部件对象,而是引用了类对象FileExplor。
fileExplorer = FileExplorer()
fileExplorer.showFullScreen()
fileExplor = FileExplor()
fileExplor.show()
您可以尝试将FileExplor
作为FileExplorer
的参数吗?
另外,尝试命名FileExplor
其他内容,例如DependentFileExplorer
(了解命名约定here)并执行此操作:
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。