0

I am doing a project in which there is a point in main class where i have to call a pyqt window with two buttons, those buttons are attached to functions which change value of global variable to "IN" or "OUT". But i am unable to exit that window after button is pressed and value is changed. Let me first show you my gui class code, look out the code for the two buttons and IN/OUT functions:

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()

now i am calling it from the same file and another class, which is main class of my project like this..

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

But the IN/OUT window doesn't exit hence "img = Image.open(court)" this line is not executed. So the issue is to make buttons act like that they change the variable, exit the window and return to the class from where window was called. Thanks

Moeed Kundi
  • 133
  • 1
  • 1
  • 10

1 Answers1

0

If I correctly remember, the method to close a windows is... close. So you should replace calls to exit with calls to close:

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

(same for OUT...)

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • ok it did close the window, but the code after 'dialog = Window() dialog.exec_()' is not working..it's like it never returned to next line of the main class after closing window of gui class – Moeed Kundi May 10 '17 at 10:05
  • That's a different problem then. You should search for *PyQt Modal Dialog*. On SO you could read http://stackoverflow.com/q/24697347/3545273 or http://stackoverflow.com/q/18196799/3545273, the latter explains how to retrieve info from the sub window. – Serge Ballesta May 10 '17 at 10:16