PyQT中的多对话框程序不会关闭
对于我的项目,我需要多个对话框相互链接。一个按钮将进入一级,另一个按钮将返回两级。为了在不显示所有代码的情况下了解我正在寻找的内容的基本概念,这里有一个可编译的示例:
'''
Created on 2010-06-18
@author: dhatt
'''
import sys
from PyQt4 import QtGui, QtCore
class WindowLV3(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(300, 300, 120, 150)
self.setWindowTitle('LV3')
quit = QtGui.QPushButton('Close', self)
quit.setGeometry(10, 10, 60, 35)
self.connect(quit, QtCore.SIGNAL('clicked()'),
QtGui.qApp, QtCore.SLOT('quit()')) # this will close entire program
class WindowLV2(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.Window3 = WindowLV3()
self.setGeometry(300, 300, 120, 150)
self.setWindowTitle('LV2')
quit = QtGui.QPushButton('Close', self)
quit.setGeometry(10, 10, 60, 35)
next = QtGui.QPushButton('Lv3', self)
next.setGeometry(10, 50, 60, 35)
self.connect(quit, QtCore.SIGNAL('clicked()'),
QtGui.qApp, QtCore.SLOT('reject()')) # this doesn't work
self.connect(next, QtCore.SIGNAL('clicked()'),
self.nextWindow)
def nextWindow(self):
self.Window3.show()
class WindowLV1(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.Window2 = WindowLV2()
self.setGeometry(300, 300, 120, 150)
self.setWindowTitle('LV1')
next = QtGui.QPushButton('Lv2', self)
next.setGeometry(10, 50, 60, 35)
quit = QtGui.QPushButton('Close', self)
quit.setGeometry(10, 10, 60, 35)
self.connect(next, QtCore.SIGNAL('clicked()'),
self.nextWindow)
def nextWindow(self):
self.Window2.show()
self.connect(quit, QtCore.SIGNAL('clicked()'),
QtGui.qApp, QtCore.SLOT('reject()')) # this doesn't work
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
Window1 = WindowLV1()
Window1.show()
sys.exit(app.exec_())
问题是我无法从窗口关闭并显示上一个窗口。例如,如果我在 LV3 窗口中单击“关闭”按钮,它会将控制权转移回 LV2 窗口。我可以调用 QtCore.SLOT('quit()')),但它会关闭整个程序,我不希望这样。
我在这里做错了什么?
For my project, I require multiple dialogs to be linked to each other. One button would go to one level, another button would go back two levels. To get a basic idea of what I'm looking for without showing all my code, here is a compilable example:
'''
Created on 2010-06-18
@author: dhatt
'''
import sys
from PyQt4 import QtGui, QtCore
class WindowLV3(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(300, 300, 120, 150)
self.setWindowTitle('LV3')
quit = QtGui.QPushButton('Close', self)
quit.setGeometry(10, 10, 60, 35)
self.connect(quit, QtCore.SIGNAL('clicked()'),
QtGui.qApp, QtCore.SLOT('quit()')) # this will close entire program
class WindowLV2(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.Window3 = WindowLV3()
self.setGeometry(300, 300, 120, 150)
self.setWindowTitle('LV2')
quit = QtGui.QPushButton('Close', self)
quit.setGeometry(10, 10, 60, 35)
next = QtGui.QPushButton('Lv3', self)
next.setGeometry(10, 50, 60, 35)
self.connect(quit, QtCore.SIGNAL('clicked()'),
QtGui.qApp, QtCore.SLOT('reject()')) # this doesn't work
self.connect(next, QtCore.SIGNAL('clicked()'),
self.nextWindow)
def nextWindow(self):
self.Window3.show()
class WindowLV1(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.Window2 = WindowLV2()
self.setGeometry(300, 300, 120, 150)
self.setWindowTitle('LV1')
next = QtGui.QPushButton('Lv2', self)
next.setGeometry(10, 50, 60, 35)
quit = QtGui.QPushButton('Close', self)
quit.setGeometry(10, 10, 60, 35)
self.connect(next, QtCore.SIGNAL('clicked()'),
self.nextWindow)
def nextWindow(self):
self.Window2.show()
self.connect(quit, QtCore.SIGNAL('clicked()'),
QtGui.qApp, QtCore.SLOT('reject()')) # this doesn't work
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
Window1 = WindowLV1()
Window1.show()
sys.exit(app.exec_())
The problem is that I cannot close from a window and show the previous window. For instance, if I clicked on the 'CLOSE' button inside from a LV3 window, it will transfer control back to a LV2 window. I can call QtCore.SLOT('quit()')), but it will shut down the entire program, and I don't want that.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两件事需要解决。
您可以简单地在connect方法中调用QDialog.close。
在
def nextWindow(self):
中,您正在尝试连接局部变量退出。所以这是行不通的。您需要将 quit 定义为实例变量(self.quit)self.connect(self.quit, QtCore.SIGNAL('clicked()'),
self.close) # 这应该可以工作
这是修改后的代码:
there are two things that need to be addressed here.
You can simply call QDialog.close in the connect method.
In
def nextWindow(self):
you are trying to connect a local variable quit. So it won't work. You need to define quit as an instance variable (self.quit)self.connect(self.quit, QtCore.SIGNAL('clicked()'),
self.close) # this should work
Here is the modified code: