[英]Exit function for a pyqt class
我正在做一個項目,在主類中有一個要點,我必須調用帶有兩個按鈕的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)”。 因此,問題在於使按鈕的行為類似於更改變量,退出窗口並從調用窗口的位置返回類。 謝謝
如果我沒有記錯的話,關閉窗口的方法是... close
。 因此,您應該將調用exit
替換為close
調用:
def IN(self):
global verdict
verdict='IN'
self.close()
(與OUT相同...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.