英文:
PyQt: How to use QMessageBox.open() and connect callback?
问题
import sys
from PyQt6.QtWidgets import (
QApplication, QMainWindow, QPushButton, QMessageBox
)
def show_dialog(app, window):
mbox = QMessageBox(window)
mbox.setIcon(QMessageBox.Icon.Information)
mbox.setText("Message box pop up window")
mbox.setWindowTitle("QMessageBox Example")
mbox.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
def button_clicked():
print("message box button clicked")
mbox.done(0)
app.quit()
# How to connect button_clicked() to the standard buttons of the message box?
mbox.open(button_clicked, button_clicked) # Use button_clicked for both receiver and member
def main():
app = QApplication(sys.argv)
window = QMainWindow()
window.resize(330, 200)
window.setWindowTitle("Testing")
button = QPushButton(window)
button.setText("Button1")
button.setGeometry(100,100,100,30)
def button_clicked():
print("button clicked")
show_dialog(app, window)
button.clicked.connect(button_clicked)
window.show()
app.exec()
if __name__ == '__main__':
main()
英文:
How can I connect a callback when using QMessageBox.open()
? In the documentation it says:
> QMessageBox.open(receiver, member)
Opens the dialog and connects its finished() or buttonClicked() signal to the slot specified by receiver and member . If the slot in member has a pointer for its first parameter the connection is to buttonClicked() , otherwise the connection is to finished() .
What should I use for receiver
and member
in the below example:
import sys
from PyQt6.QtWidgets import (
QApplication, QMainWindow, QPushButton, QMessageBox
)
def show_dialog(app, window):
mbox = QMessageBox(window)
mbox.setIcon(QMessageBox.Icon.Information)
mbox.setText("Message box pop up window")
mbox.setWindowTitle("QMessageBox Example")
mbox.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
def button_clicked():
print("message box button clicked")
mbox.done(0)
app.quit()
# How to connect button_clicked() to the standard buttons of the message box?
mbox.open(receiver, member) # What to use for receiver and member?
def main():
app = QApplication(sys.argv)
window = QMainWindow()
window.resize(330, 200)
window.setWindowTitle("Testing")
button = QPushButton(window)
button.setText("Button1")
button.setGeometry(100,100,100,30)
def button_clicked():
print("button clicked")
show_dialog(app, window)
button.clicked.connect(button_clicked)
window.show()
app.exec()
if __name__ == '__main__':
main()
答案1
得分: 0
After some trial and error I found that I could simply use mbox.open(button_clicked)
but I am still not sure how I can determine from within the callback which of the two buttons was clicked. Also one should not call QMessageBox.done()
from within the callback since that would lead to infinite recursion. Here is how I call QMessageBox.open()
which works, but I am still missing the last piece of the puzzle: How to determine which button was clicked?
def show_dialog(app, window):
mbox = QMessageBox(window)
mbox.setIcon(QMessageBox.Icon.Information)
mbox.setText("Message box pop up window")
mbox.setWindowTitle("QMessageBox Example")
mbox.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
#@pyqtSlot() # It is not necessary to use this decorator?
def button_clicked():
print("message box button clicked") # But which button?
#mbox.done(0) # Do not call this method as it will lead to infinite recursion
app.quit()
mbox.open(button_clicked)
print("Dialog opened")
Update:
How to determine which button was clicked?
You can use mbox.clickedButton().text()
, for example like this:
def button_clicked():
if mbox.clickedButton().text() == "&OK":
print("OK clicked")
else:
print("Cancel clicked")
app.quit()
英文:
After some trial and error I found that I could simply use mbox.open(button_clicked)
but I am still not sure how I can determine from within the callback which of the two buttons was clicked. Also one should not call QMessageBox.done()
from within the callback since that would lead to infinite recursion. Here is how I call QMessageBox.open()
which works, but I am still missing the last piece of the puzzle: How to determine which button was clicked?
def show_dialog(app, window):
mbox = QMessageBox(window)
mbox.setIcon(QMessageBox.Icon.Information)
mbox.setText("Message box pop up window")
mbox.setWindowTitle("QMessageBox Example")
mbox.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
#@pyqtSlot() # It is not necessary to use this decorator?
def button_clicked():
print("message box button clicked") # But which button?
#mbox.done(0) # Do not call this method as it will lead to infinite recursion
app.quit()
mbox.open(button_clicked)
print("Dialog opened")
Update:
> How to determine which button was clicked?
You can use mbox.clickedButton().text()
, for example like this:
def button_clicked():
if mbox.clickedButton().text() == "&OK":
print("OK clicked")
else:
print("Cancel clicked")
app.quit()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论