I thought I'll try pyqt(5) for a change and wanted to create a simple dialog on startup of a script that let's the user choose one of three options.
My goal is a popup like this (on startup of a script) that will close on pushing one of the buttons and returns the value of the button that was pressed:
And this is how I tried to implement it:
from PyQt5.QtWidgets import QApplication, QDialog, QPushButton, QDialogButtonBox
input_db_connection="None"
class MyDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Choose Database connection")
self.buttonBox=QDialogButtonBox(self)
for option in ["Production Read", "Production Read&Write", "Development"]:
b = QPushButton(option)
b.clicked.connect(lambda x: return_and_close(option))
self.buttonBox.addButton(b,0)
del b
def return_and_close(value):
global input_db_connection
print(value)
input_db_connection=value
self.close()
app = QApplication([])
dg = MyDialog()
dg.show()
app.exec()
print(input_db_connection)
I used a global variable, as I didn't find a way to return anything from the QApplication.
No matter which button I press, the output is "Development", the last option that was entered?
