import sys
from PySide2.QtWidgets import QApplication, QWidget # required pyside2 == 5.11.2
def run_app(function):
def wrapper(*args, **kwargs):
app = QApplication(sys.argv)
function(*args, **kwargs)
sys.exit(app.exec_())
return wrapper
@run_app
def show_widget():
"""not work as expected
:return:
"""
widget = QWidget()
widget.show()
if __name__ == "__main__":
show_widget()
What should i do to make the decorator work properly?
The problem is that widget is a local variable that is destroyed as soon as the function is finished executing. A possible solution is for the function to return the widget and be stored in a variable to extend its scope.
def run_app(function):
def wrapper(*args, **kwargs):
app = QApplication(sys.argv)
obj = function(*args, **kwargs)
sys.exit(app.exec_())
return wrapper
@run_app
def show_widget():
widget = QWidget()
widget.show()
return widget