pyqt类的退出函数

时间:2017-05-10 09:12:09

标签: python python-2.7 user-interface pyqt pyqt4

我正在做一个项目,其中在主类中有一个点,我必须调用带有两个按钮的pyqt窗口,这些按钮附加到将全局变量的值更改为" IN"或" OUT"。但按下按钮后,我无法退出该窗口并更改了值。让我先向您展示我的gui类代码,查看两个按钮和IN / OUT功能的代码:

class Window(QtGui.QMainWindow):
def __init__(self):
    self.window_exec = None
    self.window_about = None
    self.window_help = None

    super(Window, self).__init__()
    self.setGeometry(0, 0, 500, 400)
    self.setWindowTitle('Court Selection')
    self.setStyleSheet("background-color:#003333")
    #palette    = QtGui.QPalette()
    #palette.setBrush(QtGui.QPalette.Background,QtGui.QBrush(QtGui.QPixmap("pic1.jpg")))
    #self.setPalette(palette)
    self.center()

    self.default_layout()

    self.show()

# Positioning window in the center
def center(self):
    frameGm = self.frameGeometry()
    screen = QtGui.QApplication.desktop().screenNumber(QtGui.QApplication.desktop().cursor().pos())
    centerPoint = QtGui.QApplication.desktop().screenGeometry(screen).center()
    frameGm.moveCenter(centerPoint)
    self.move(frameGm.topLeft())

def default_layout(self):
    # ----------- Main label -----------
    lbl = QtGui.QLabel("Please Select the Verdict", self)
    lbl.move(100, 70)
    lbl.setStyleSheet('font-size:20pt;color:white')
    lbl.resize(lbl.sizeHint())
    # ----------- IN Button -----------
    btn = QtGui.QPushButton("IN", self)
    btn.clicked.connect(self.IN)
    btn.setStyleSheet('font-size:12pt;background-color:white;border-radius:5px;')
    btn.resize(QtCore.QSize(100, 50))
    btn.move(200, 170)
    # ----------- OUT Button -----------
    btn = QtGui.QPushButton("OUT", self)
    btn.clicked.connect(self.OUT)
    btn.setStyleSheet('font-size:12pt;background-color:white;border-radius:5px;')
    btn.resize(QtCore.QSize(100, 50))
    btn.move(200, 250)

def IN(self):
    global verdict
    verdict='IN'
    self.exit()

def OUT(self):
    global verdict
    verdict='OUT'
    self.exit()

现在我从同一个文件和另一个类调用它,这是我项目的主要类,如下所示..

 dialog = Window()
 dialog.exec_()
 img = Image.open(court)

但IN / OUT窗口不会退出,因此" img = Image.open(court)"此行未执行。所以问题是让按钮的行为就像他们改变变量一样,退出窗口并返回调用窗口的类。感谢

1 个答案:

答案 0 :(得分:0)

如果我没记错,关闭窗口的方法是...... close。因此,您应该通过调用exit来替换对close的来电:

def IN(self):
    global verdict
    verdict='IN'
    self.close()

(对于OUT ......)