获取第二个 window 将变量传递给主 ui 并关闭
Getting a second window pass a variable to the main ui and close
我试图从列表中选择一个变量发送回主window,然后在按下确定按钮时关闭第二个window。目前第二个 window 不会关闭。任何帮助将不胜感激。
代码:
class select_variable_window(QDialog):
def __init__(self, list, parent=None):
super().__init__(parent)
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(QtCore.QRect(20, 10, 251, 16))
self.label.setObjectName("label")
self.label=QLabel("Select Variable for Scatterplot", self)
self.dialogbutton = QtWidgets.QDialogButtonBox(self)
self.dialogbutton.setGeometry(QtCore.QRect(120, 260, 161, 32))
self.dialogbutton.setOrientation(QtCore.Qt.Horizontal)
self.dialogbutton.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.dialogbutton.setObjectName("dialogbutton")
self.variablelist = QtWidgets.QListWidget(self)
self.variablelist.setGeometry(QtCore.QRect(10, 30, 391, 221))
self.variablelist.setObjectName("variablelist")
self.variablelist.setSelectionMode(QAbstractItemView.SingleSelection)
self.variablelist.addItems(list)
self.dialogbutton.accepted.connect(self.accept)
self.dialogbutton.rejected.connect(self.reject)
QtCore.QMetaObject.connectSlotsByName(self)
def accept(self):
self.scattervariable=[item.text() for item in self.variablelist.selectedItems()]
print(self.scattervariable)
self.close()# wont close
return
def reject(self):
self.close()#wont close
return
首先不要使用list作为变量名,因为它是一个保留字,这样做被认为是一种不好的做法,将来会带来问题。
另一方面,accept()
是一种关闭 window 并给出 return 代码 QDialog.Accepted
的方法,但您正在覆盖它并阻止它执行它的工作,所以在完成你的逻辑之后,你必须调用父的方法来完成他的典型关闭并建立一个 return 代码。
另一方面,建议 类 的名称以大写字母开头,阅读 PEP8 了解更多信息。另一方面,我建议学习使用布局来确定元素的位置。
from PyQt5 import QtCore, QtWidgets
class Select_variable_window(QtWidgets.QDialog):
def __init__(self, items, parent=None):
super().__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
label = QtWidgets.QLabel("Select Variable for Scatterplot:")
self.variablelist = QtWidgets.QListWidget()
self.variablelist.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
self.variablelist.addItems(items)
dialogbutton = QtWidgets.QDialogButtonBox()
dialogbutton.setOrientation(QtCore.Qt.Horizontal)
dialogbutton.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
lay.addWidget(label)
lay.addWidget(self.variablelist)
lay.addWidget(dialogbutton)
dialogbutton.accepted.connect(self.accept)
dialogbutton.rejected.connect(self.reject)
def accept(self):
self.scattervariable = [item.text() for item in self.variablelist.selectedItems()]
super().accept() # <-- call parent method
class FirstWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
button = QtWidgets.QPushButton("Open Dialog")
button.clicked.connect(self.on_clicked)
lay.addWidget(button)
@QtCore.pyqtSlot()
def on_clicked(self):
w = Select_variable_window(["1", "2", "3", "4"])
if w.exec_() == QtWidgets.QDialog.Accepted:
print(w.scattervariable)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = FirstWidget()
w.show()
sys.exit(app.exec_())
我试图从列表中选择一个变量发送回主window,然后在按下确定按钮时关闭第二个window。目前第二个 window 不会关闭。任何帮助将不胜感激。
代码:
class select_variable_window(QDialog):
def __init__(self, list, parent=None):
super().__init__(parent)
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(QtCore.QRect(20, 10, 251, 16))
self.label.setObjectName("label")
self.label=QLabel("Select Variable for Scatterplot", self)
self.dialogbutton = QtWidgets.QDialogButtonBox(self)
self.dialogbutton.setGeometry(QtCore.QRect(120, 260, 161, 32))
self.dialogbutton.setOrientation(QtCore.Qt.Horizontal)
self.dialogbutton.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.dialogbutton.setObjectName("dialogbutton")
self.variablelist = QtWidgets.QListWidget(self)
self.variablelist.setGeometry(QtCore.QRect(10, 30, 391, 221))
self.variablelist.setObjectName("variablelist")
self.variablelist.setSelectionMode(QAbstractItemView.SingleSelection)
self.variablelist.addItems(list)
self.dialogbutton.accepted.connect(self.accept)
self.dialogbutton.rejected.connect(self.reject)
QtCore.QMetaObject.connectSlotsByName(self)
def accept(self):
self.scattervariable=[item.text() for item in self.variablelist.selectedItems()]
print(self.scattervariable)
self.close()# wont close
return
def reject(self):
self.close()#wont close
return
首先不要使用list作为变量名,因为它是一个保留字,这样做被认为是一种不好的做法,将来会带来问题。
另一方面,accept()
是一种关闭 window 并给出 return 代码 QDialog.Accepted
的方法,但您正在覆盖它并阻止它执行它的工作,所以在完成你的逻辑之后,你必须调用父的方法来完成他的典型关闭并建立一个 return 代码。
另一方面,建议 类 的名称以大写字母开头,阅读 PEP8 了解更多信息。另一方面,我建议学习使用布局来确定元素的位置。
from PyQt5 import QtCore, QtWidgets
class Select_variable_window(QtWidgets.QDialog):
def __init__(self, items, parent=None):
super().__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
label = QtWidgets.QLabel("Select Variable for Scatterplot:")
self.variablelist = QtWidgets.QListWidget()
self.variablelist.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
self.variablelist.addItems(items)
dialogbutton = QtWidgets.QDialogButtonBox()
dialogbutton.setOrientation(QtCore.Qt.Horizontal)
dialogbutton.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
lay.addWidget(label)
lay.addWidget(self.variablelist)
lay.addWidget(dialogbutton)
dialogbutton.accepted.connect(self.accept)
dialogbutton.rejected.connect(self.reject)
def accept(self):
self.scattervariable = [item.text() for item in self.variablelist.selectedItems()]
super().accept() # <-- call parent method
class FirstWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
button = QtWidgets.QPushButton("Open Dialog")
button.clicked.connect(self.on_clicked)
lay.addWidget(button)
@QtCore.pyqtSlot()
def on_clicked(self):
w = Select_variable_window(["1", "2", "3", "4"])
if w.exec_() == QtWidgets.QDialog.Accepted:
print(w.scattervariable)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = FirstWidget()
w.show()
sys.exit(app.exec_())