如何在 python 中销毁 pyqt4 中的小部件
How to destroy widget in pyqt4 in python
我最近从 tkinter 切换到 pyqt4,也许这个问题是重复的,但我找不到工作 example/answer。在我的应用程序中,我有控制小部件输入和更新 DB.It 的按钮,效果很好,但我不想制作类似 tkinter 的 .askyesno(或 html 模态)这样的东西来询问用户是否he/she 确定该命令。
def modal(self):
d = QtGui.QDialog()
d.setModal( True )
b1 = QtGui.QPushButton("ok",d)
b1.move(50,50)
b1.clicked.connect(lambda event: self.yes(d))
d.setWindowTitle("Dialog")
d.setWindowModality(QtCore.Qt.ApplicationModal)
d.exec_()
print('b1 ans', self.ans)
def yes(self, d):
self.ans = True
d.setParent(None)
现在,当我单击按钮时,它会调用模态函数,使 QDialog 带有按钮 it.Now 我的问题是,因为我已经设置了 setModal(True) 程序等待 QDialog 退出,但它退出了仅在小的红色 x/ 关闭按钮上,我需要 QDialog 退出/删除/销毁按钮单击的任何内容,例如在函数中 'yes'?
是否可能(我猜是),以及如何。
提前致谢。
你想要的,QMessageBox提供。这是一个带有“是”、“否”和“取消”按钮的小示例,您可以为其指定自定义名称。
confirm = QtGui.QMessageBox()
confirm.setWindowTitle("Are you sure?")
confirm.setText("Please choose an option")
btnYes = confirm.addButton("Yes", QtGui.QMessageBox.YesRole)
btnNo = confirm.addButton("No", QtGui.QMessageBox.NoRole)
btnCancel = confirm.addButton("Cancel", QtGui.QMessageBox.RejectRole)
confirm.setDefaultButton(btnYes)
confirm = confirm.exec_()
我最近从 tkinter 切换到 pyqt4,也许这个问题是重复的,但我找不到工作 example/answer。在我的应用程序中,我有控制小部件输入和更新 DB.It 的按钮,效果很好,但我不想制作类似 tkinter 的 .askyesno(或 html 模态)这样的东西来询问用户是否he/she 确定该命令。
def modal(self):
d = QtGui.QDialog()
d.setModal( True )
b1 = QtGui.QPushButton("ok",d)
b1.move(50,50)
b1.clicked.connect(lambda event: self.yes(d))
d.setWindowTitle("Dialog")
d.setWindowModality(QtCore.Qt.ApplicationModal)
d.exec_()
print('b1 ans', self.ans)
def yes(self, d):
self.ans = True
d.setParent(None)
现在,当我单击按钮时,它会调用模态函数,使 QDialog 带有按钮 it.Now 我的问题是,因为我已经设置了 setModal(True) 程序等待 QDialog 退出,但它退出了仅在小的红色 x/ 关闭按钮上,我需要 QDialog 退出/删除/销毁按钮单击的任何内容,例如在函数中 'yes'?
是否可能(我猜是),以及如何。 提前致谢。
你想要的,QMessageBox提供。这是一个带有“是”、“否”和“取消”按钮的小示例,您可以为其指定自定义名称。
confirm = QtGui.QMessageBox()
confirm.setWindowTitle("Are you sure?")
confirm.setText("Please choose an option")
btnYes = confirm.addButton("Yes", QtGui.QMessageBox.YesRole)
btnNo = confirm.addButton("No", QtGui.QMessageBox.NoRole)
btnCancel = confirm.addButton("Cancel", QtGui.QMessageBox.RejectRole)
confirm.setDefaultButton(btnYes)
confirm = confirm.exec_()