Python QT Application with vlc does not show fullscreen - python-3.x

I am working on an application where several vlc streams (rtsp) are shown and by double-clicking one of them, the stream should be displayed fullscreen.
The application is python 3.7 using pyqt5 and vlc-qt.
Code as follows:
import sys
import vlc
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.sizeHint = lambda: QSize(1280, 900)
self.move(100, 10)
self.videoFrame = QFrame()
self.setCentralWidget(self.videoFrame)
self.vlcInstance = vlc.Instance(['--video-on-top'])
self.videoPlayer = self.vlcInstance.media_player_new()
self.videoPlayer.set_mrl("rtsp://xxx.xxx.xxx.xxx", "network-caching=300")
self.videoPlayer.audio_set_mute(True)
if sys.platform.startswith('linux'): # for Linux using the X Server
self.videoPlayer.set_xwindow(self.videoFrame.winId())
elif sys.platform == "win32": # for Windows
self.videoPlayer.set_hwnd(self.videoFrame.winId())
elif sys.platform == "darwin": # for MacOS
self.videoPlayer.set_nsobject(int(self.videoFrame.winId()))
self.videoPlayer.play()
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setApplicationName("VLC Test")
window = MainWindow()
app.exec_()
When double clicking the video, the following console message appears:
[000001e0a128e630] mmdevice audio output error: cannot initialize COM (error 0x80010106)
[000001e0a12c8710] mmdevice audio output error: cannot initialize COM (error 0x80010106)
[000001e0a2927420] main vout display error: Failed to set fullscreen
The message "Failed to set fullscreen" appears as soon as I double-click.
Does anyone have an idea what the problem might be?
Thanks in advance

import sys
import vlc
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.sizeHint = lambda: QSize(1280, 900)
self.move(100, 10)
self.mainFrame = QFrame()
self.setCentralWidget(self.mainFrame)
t_lay_parent = QHBoxLayout()
t_lay_parent.setContentsMargins(0, 0, 0, 0)
self.videoFrame = QFrame()
self.videoFrame.mouseDoubleClickEvent = self.mouseDoubleClickEvent
t_lay_parent.addWidget(self.videoFrame)
self.vlcInstance = vlc.Instance(['--video-on-top'])
self.videoPlayer = self.vlcInstance.media_player_new()
self.videoPlayer = self.vlcInstance.media_player_new()
self.videoPlayer.video_set_mouse_input(False)
self.videoPlayer.video_set_key_input(False)
self.videoPlayer.set_mrl("http://xxx.xxx.xxx.xxx", "network-caching=300")
self.videoPlayer.audio_set_mute(True)
if sys.platform.startswith('linux'): # for Linux using the X Server
self.videoPlayer.set_xwindow(self.videoFrame.winId())
elif sys.platform == "win32": # for Windows
self.videoPlayer.set_hwnd(self.videoFrame.winId())
elif sys.platform == "darwin": # for MacOS
self.videoPlayer.set_nsobject(int(self.videoFrame.winId()))
self.videoPlayer.play()
self.videoFrame1 = QFrame()
t_lay_parent.addWidget(self.videoFrame1)
self.videoFrame1.mouseDoubleClickEvent = self.mouseDoubleClickEvent1
self.vlcInstance1 = vlc.Instance(['--video-on-top'])
self.videoPlayer1 = self.vlcInstance1.media_player_new()
self.videoPlayer1 = self.vlcInstance1.media_player_new()
self.videoPlayer1.video_set_mouse_input(False)
self.videoPlayer1.video_set_key_input(False)
self.videoPlayer1.set_mrl("rtmp://xxx.xxx.xxx.xxx", "network-caching=300")
self.videoPlayer1.audio_set_mute(True)
if sys.platform.startswith('linux'): # for Linux using the X Server
self.videoPlayer1.set_xwindow(self.videoFrame1.winId())
elif sys.platform == "win32": # for Windows
self.videoPlayer1.set_hwnd(self.videoFrame1.winId())
elif sys.platform == "darwin": # for MacOS
self.videoPlayer1.set_nsobject(int(self.videoFrame1.winId()))
self.videoPlayer1.play()
self.mainFrame.setLayout(t_lay_parent)
self.show()
def mouseDoubleClickEvent(self, event):
if event.button() == Qt.LeftButton:
if self.windowState() == Qt.WindowNoState:
self.videoFrame1.hide()
self.videoFrame.show()
self.setWindowState(Qt.WindowFullScreen)
else:
self.videoFrame1.show()
self.setWindowState(Qt.WindowNoState)
def mouseDoubleClickEvent1(self, event):
if event.button() == Qt.LeftButton:
if self.windowState() == Qt.WindowNoState:
self.videoFrame.hide()
self.videoFrame1.show()
self.setWindowState(Qt.WindowFullScreen)
else:
self.videoFrame.show()
self.setWindowState(Qt.WindowNoState)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setApplicationName("VLC Test")
window = MainWindow()
app.exec_()

Related

PySide2 works not efficient

from PySide2.QtWidgets import QApplication, QMainWindow,QDialog,QLineEdit,QPushButton
from PySide2.QtCore import Qt
import sys
from ui618 import Ui_MainWindow
from numpad import Ui_Dialog
class MyWindow:
def __init__(self) -> None:
self.lastSelectedEditLine = None
# main ui
self.MainWindow = QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.MainWindow)
# numpad
self.QDialog = QDialog(self.MainWindow)
self.numpad = Ui_Dialog()
self.numpad.setupUi(self.QDialog)
self.QDialog.setWindowFlags(Qt.Popup)
# bind editline with numpad
self.lineEdits = self.MainWindow.findChildren(QLineEdit)
for item in self.lineEdits:
item.focusInEvent = lambda e,item=item:self.test(e,item)
def save_quit(self):
sys.exit()
def bindbtn(self):
self.ui.quit.clicked.connect(self.save_quit)
def numpad_btn(self,a,item):
if item.text() != '←':
self.lastSelectedEditLine.setText(self.lastSelectedEditLine.text()+item.text())
else:
self.lastSelectedEditLine.setText(self.lastSelectedEditLine.text()[:-1])
def bindNumpad(self):
numpad_btns = self.QDialog.findChildren(QPushButton)
for item in numpad_btns:
item.clicked.connect(lambda ignore='ignore',item=item : self.numpad_btn(ignore,item))
def test(self,e,item):
self.lastSelectedEditLine = item
this = item
x = self.MainWindow.x()
y = self.MainWindow.y() + self.ui.selectX.rect().bottom()
while this.parentWidget().objectName() != 'MainWindow':
x += this.x()
y += this.y()
this = this.parentWidget()
self.QDialog.move(x, y)
self.QDialog.show()
item.clearFocus()
def show(self):
self.MainWindow.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
myWindow.bindbtn()
myWindow.bindNumpad()
sys.exit(app.exec_())
The code works as expected, the numpad shows while focusing in a qlineedit. and can write and delete to target input line.
I am new to pyside2. Is there a better way to implement the numpad? I this way, the cursor is disable, I have to edit at the end of each editline.
It take several seconds while starting, Some one know how to improve it?

Connecting external threaded log to PyQt5 QPlainTextEdit

I'm new to PyQt and handlers in general, I tried to read from every repo I found but I can't figure out how to fix my issue, as you can see in my code, I'm executing logging thread in the background and I'm trying to show the logs in my QPlainTextEdit console - for some reason I can see the logs in my terminal and the text_box doesn't get the logs at all,
I will appreciate a lot your smart help.
import pandas as pd
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import time
import os
import threading
import json
class ConsolePanelHandler(logging.Handler):
def __init__(self, stream):
# super().__init__()
logging.Handler.__init__(self)
# logging.StreamHandler.__init__(self, stream)
self.stream = stream
def handle(self, record):
rv = self.filter(record)
if rv:
self.acquire()
try:
self.emit(record)
finally:
self.release()
return rv
def emit(self, record):
try:
stream = self.stream
stream(self.format(record))
except RecursionError:
raise
except Exception:
self.handleError(self.format(record))
def thread():
for index in range(20):
logging.warning('scheiBe '+str(index))
time.sleep(1)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.continue_ = QPushButton("Continue")
self.continue_.setStyleSheet("background-color: green")
self.continue_.setFont(QFont('SansSerif', 10))
self.continue_.setFixedSize(QSize(300, 22))
self.pause = QPushButton("Pause")
self.pause.setStyleSheet("background-color: orange")
self.pause.setFont(QFont('SansSerif', 10))
self.pause.setFixedSize(QSize(300, 22))
self.stop = QPushButton("Stop")
self.stop.setStyleSheet("background-color: #FD4B4B")
self.stop.setFont(QFont('SansSerif', 10))
self.stop.setFixedSize(QSize(300, 22))
self.text_box = QPlainTextEdit()
self.text_box.setPlaceholderText("Bugs will be printed here")
self.text_box.setReadOnly(True)
logging.getLogger().addHandler(self.text_box)
logging.getLogger().setLevel(logging.DEBUG)
ConsolePanelHandler(self.appendDebug , logging.DEBUG)
self.text_box.moveCursor(QTextCursor.End)
layout.addWidget(self.continue_)
layout.addWidget(self.pause)
layout.addWidget(self.stop)
layout.addWidget(self.text_box)
self.w = QWidget()
self.w.setLayout(layout)
self.setCentralWidget(self.w)
thread1 = threading.Thread(target=thread, args=(), daemon=True)
thread1.start()
self.show()
def closeEvent(self, event):
close = QMessageBox()
close.setText("Are you sure want to stop and exit?")
close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
close = close.exec()
if close == QMessageBox.Yes:
sys.exit()
else:
event.ignore()
def appendDebug(self, string):
self.text_box.appendPlainText(string +'\n')
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
sys.exit(app.exec())
First, you should not pass the function that updates the text_area instead you should create a pyqtSignal which updates the text_area and pass it to ConsolePanelHandler.
Add ConsolePanelHandler as handler not text_area to logging.getLogger().addHandler()
and it is recommended to use QThread instead of threading.Thread
here is the complete updated code.
# import pandas as pd
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import time
import os
import sys
import threading
import json
import logging
class ConsolePanelHandler(logging.Handler):
def __init__(self, sig):
# super().__init__()
logging.Handler.__init__(self)
# logging.StreamHandler.__init__(self, stream)
self.stream = sig
def handle(self, record):
rv = self.filter(record)
if rv:
self.acquire()
try:
self.emit(record)
finally:
self.release()
return rv
def emit(self, record):
try:
self.stream.emit(self.format(record))
except RecursionError:
raise
except Exception:
self.handleError(record)
class thread(QThread):
def run(self) -> None:
for index in range(20):
logging.warning('scheiBe ' + str(index))
self.sleep(1)
class MainWindow(QMainWindow):
sig = pyqtSignal(str)
def __init__(self):
super().__init__()
self.layout = QVBoxLayout()
self.continue_ = QPushButton("Continue")
self.continue_.setStyleSheet("background-color: green")
self.continue_.setFont(QFont('SansSerif', 10))
self.continue_.setFixedSize(QSize(300, 22))
self.pause = QPushButton("Pause")
self.pause.setStyleSheet("background-color: orange")
self.pause.setFont(QFont('SansSerif', 10))
self.pause.setFixedSize(QSize(300, 22))
self.stop = QPushButton("Stop")
self.stop.setStyleSheet("background-color: #FD4B4B")
self.stop.setFont(QFont('SansSerif', 10))
self.stop.setFixedSize(QSize(300, 22))
self.c = ConsolePanelHandler(self.sig)
self.text_box = QPlainTextEdit()
self.text_box.setPlaceholderText("Bugs will be printed here")
self.text_box.setReadOnly(True)
logging.getLogger().addHandler(self.c)
logging.getLogger().setLevel(logging.DEBUG)
self.sig.connect(self.appendDebug)
self.text_box.moveCursor(QTextCursor.End)
self.layout.addWidget(self.continue_)
self.layout.addWidget(self.pause)
self.layout.addWidget(self.stop)
self.layout.addWidget(self.text_box)
self.w = QWidget()
self.w.setLayout(self.layout)
self.setCentralWidget(self.w)
self.thread1 = thread(self) # self is parent for Qthread so Qthread will be destroyed when it's parent no longer exist
self.thread1.start()
self.show()
def closeEvent(self, event):
close = QMessageBox()
close.setText("Are you sure want to stop and exit?")
close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
close = close.exec()
if close == QMessageBox.Yes:
self.thread1.terminate()
sys.exit()
else:
event.ignore()
#pyqtSlot(str)
def appendDebug(self, string):
self.text_box.appendPlainText(string + '\n')
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
sys.exit(app.exec())

How can I take a value from a line edit to another window's line edit by using Python and PyQt5?

What I need:
I need to create a simple project that can take value from window to another window
My research effort:
So, I create two classes for two windows then connect to classes with each other, so when I click in button it takes the value from 1st window then open the other window but the value=nothing because the clickMethod returns nothing.
Below is my code:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(320, 140))
self.setWindowTitle("First Window")
self.nameLabel = QLabel(self)
self.nameLabel.setText('1st:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
pybutton = QPushButton('OK', self)
pybutton.clicked.connect(self.second_wind) #connect button to open second window
pybutton.clicked.connect(self.clickMethod)
pybutton.resize(200,32)
pybutton.move(80, 60)
def clickMethod(self):
value =self.line.text() #take value from the line edit
return value
def second_wind(self): #object from secod_window class
self.SW = Second_Window()
self.SW.show()
class Second_Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(320, 140))
self.setWindowTitle("Second Window")
self.nameLabel = QLabel(self)
self.nameLabel.setText('Name:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
self.f = MainWindow() #make object from MainWindow class to execute clickMethod() to reutrn value
a=self.f.clickMethod()
print(a)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )
I expect the Clickmethod to return the value
but it returns nothing
Try it:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(320, 140))
self.setWindowTitle("First Window")
self.nameLabel = QLabel(self)
self.nameLabel.setText('1st:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
pybutton = QPushButton('OK', self)
pybutton.clicked.connect(self.second_wind)
# pybutton.clicked.connect(self.clickMethod)
pybutton.resize(200,32)
pybutton.move(80, 60)
# def clickMethod(self):
# value =self.line.text()
# return value
def second_wind(self):
text = self.line.text() # +++
self.SW = Second_Window(text) # +++ (text)
self.SW.show()
class Second_Window(QMainWindow):
def __init__(self, text): # +++ (text)
QMainWindow.__init__(self)
self.text = text # +
self.setMinimumSize(QSize(320, 140))
self.setWindowTitle("Second Window")
self.nameLabel = QLabel(self)
self.nameLabel.setText('Name:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
# self.f = MainWindow()
# a=self.f.clickMethod()
self.line.setText(self.text) # +
print(self.text)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )

How can I make a circle bounce in a window?

I just made this program as a practice I want to make the circle bounce from end to end which I came up in this source codes can you make if happen.
import PyQt5,sys
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtCore import Qt,QTimer,QPoint
from PyQt5.QtWidgets import QWidget,QApplication,QMainWindow
from PyQt5.QtGui import QPainter
class Circle(QWidget):
def __init__(self):
QMainWindow.__init__(self)
self.resize(250,500)
self.setWindowTitle("Bounce-Man")
self.color = Qt.red
self.fixedplace = 125
self.mover = 450
def paintEvent(self,event):
bouncer.setPen(Qt.black)
bouncer.setBrush(self.color)
bouncer.drawEllipse(QPoint(self.fixedplace,self.mover),50,50)
#QtCore.pyqtSlot()
def timer(self):
timer = QTimer()
while self.mover >=50:
timer.start(1000)
if self.mover == 50:
self.mover = 450
self.mover -= 1
self.update()
if __name__ == "__main__":
window = QApplication(sys.argv)
app = Circle()
app.show()
sys.exit( window.exec_() )
You must use a QTimer to call a function that updates the position, do not use a while loop:
from PyQt5 import QtCore, QtGui, QtWidgets
class Circle(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Circle, self).__init__(parent)
self.resize(250,500)
self.setWindowTitle("Bounce-Man")
self.color = QtGui.QColor(QtCore.Qt.red)
self.rect_circle = QtCore.QRect(0, 0, 50, 50)
self.rect_circle.moveCenter(QtCore.QPoint(self.width()/2, self.rect_circle.height()/2))
self.step = QtCore.QPoint(0, 5)
self.y_direction = 1
timer = QtCore.QTimer(self, interval=30)
timer.timeout.connect(self.update_position)
timer.start()
def paintEvent(self,event):
bouncer = QtGui.QPainter(self)
bouncer.setPen(QtCore.Qt.black)
bouncer.setBrush(self.color)
bouncer.drawEllipse(self.rect_circle)
#QtCore.pyqtSlot()
def update_position(self):
if self.rect_circle.bottom() > self.height() and self.y_direction == 1:
self.y_direction = -1
if self.rect_circle.top() < 0 and self.y_direction == -1:
self.y_direction = 1
self.rect_circle.translate(self.step * self.y_direction)
self.update()
if __name__ == "__main__":
import sys
window = QtWidgets.QApplication(sys.argv)
app = Circle()
app.show()
sys.exit( window.exec_() )

Spin box not showing up

I'm unsure why all aspects of my GUI are showing up apart from the spin box (the code for it is within the home function).
I've tried moving it to the init(self) function, but that doesn't work. I thought it would be intuitive for it to be within the home function as that is where all my other GUI (e.g. buttons) resides.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox
from temperature import MplWindow
from filament import MplWindow1
from highvoltage import MplWindow2
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Temperature Control')
self.setWindowIcon(QIcon('adn.png'))
extractAction = QAction('&Quit', self)
extractAction.setShortcut('Ctrl+Q')
extractAction.setStatusTip('leave the app')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(extractAction)
self.matplWindow = MplWindow()
self.matplWindow1 = MplWindow1()
self.matplWindow2 = MplWindow2()
self.home()
def home(self):
btn = QPushButton('quit', self)
btn.clicked.connect(self.close_application)
btn.resize(btn.sizeHint())
btn.move(200, 260)
button = QPushButton('Temperature',self)
button.clicked.connect(self.opengraph)
button.move(100,50)
button = QPushButton('Filament voltage',self)
button.clicked.connect(self.openfilament)
button.move(100,80)
button = QPushButton('High voltage',self)
button.clicked.connect(self.openhigh)
button.move(100,110)
self.doubleSpinBox = QtWidgets.QDoubleSpinBox()
self.doubleSpinBox.setGeometry(180, 110, 62, 22)
self.show()
def opengraph(self):
self.matplWindow.funAnimation()
def openfilament(self):
self.matplWindow1.funAnimation1()
def openhigh(self):
self.matplWindow2.funAnimation2()
def close_application(self):
choice = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if choice == QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
Gui = window()
sys.exit(app.exec_())
I worked it out - I moved the code to the init function.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QAction, QMessageBox, QDoubleSpinBox, QLabel, QVBoxLayout
from temperature import MplWindow # +++
from filament import MplWindow1
from highvoltage import MplWindow2
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(50, 50, 300, 300)
self.setWindowTitle('Temperature Control')
self.setWindowIcon(QIcon('adn.png'))
extractAction = QAction('&Quit', self)
extractAction.setShortcut('Ctrl+Q')
extractAction.setStatusTip('leave the app')
extractAction.triggered.connect(self.close_application)
self.statusBar()
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(extractAction)
self.matplWindow = MplWindow() # +++
self.matplWindow1 = MplWindow1()
self.matplWindow2 = MplWindow2()
# vBoxLayout = QVBoxLayout()
self.label = QLabel("Set point Temp:", self)
self.label.move(50,150)
self.spinBox = QDoubleSpinBox(self)
self.spinBox.move(70,150)
self.home()
def home(self):
btn = QPushButton('quit', self)
btn.clicked.connect(self.close_application)
btn.resize(btn.sizeHint())
btn.move(200, 260)
button = QPushButton('Temperature',self)
button.clicked.connect(self.opengraph)
button.move(100,50)
button = QPushButton('Filament voltage',self)
button.clicked.connect(self.openfilament)
button.move(100,80)
button = QPushButton('High voltage',self)
button.clicked.connect(self.openhigh)
button.move(100,110)
self.show()
def opengraph(self):
self.matplWindow.funAnimation() # +++
def openfilament(self):
self.matplWindow1.funAnimation1()
def openhigh(self):
self.matplWindow2.funAnimation2()
def close_application(self):
choice = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if choice == QMessageBox.Yes:
sys.exit()
else:
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
Gui = window()
sys.exit(app.exec_())

Resources