[英]PyQt5 QInputDialog not showing
我正在擴展QInputDialog,我想在用戶按下某個快捷鍵時打開它。 我可以看到快捷方式已運行,並且代碼可以通過show()
方法正確運行,但是從未顯示QInputDialog。
僅當我嘗試通過快捷方式打開QInputDialog時,才會發生這種情況,如果我只是將QInputDialog放入主方法中,則可以正常運行。
class CommandPopup(QInputDialog):
""" popup for a single-line command to be entered"""
def __init__(self):
super().__init__()
self.setupGUI()
self.command_runner = commands.CommandRunner()
def setupGUI(self):
self.setLabelText("Command:")
self.show()
def done(self, result):
super().done(result)
print("done")
if result == 1:
print(self.textValue())
self.command_runner.run(self.textValue())
當我將其放在主函數中時,此方法有效
if __name__ == '__main__':
app = QApplication(sys.argv)
ui = CommandPopup()
sys.exit(app.exec_())
但是,當我嘗試從快捷方式上的另一個函數調用代碼時,它不會顯示輸入對話框。
self.textArea.shortcut = QShortcut(QKeySequence("CTRL+E"),self)
self.textArea.shortcut.activated.connect(self.command_popup)
與:
def command_popup(self):
x = CommandPopup()
(因此,縮進有點混亂,但縮進是正確的,如果我在self.show()
方法之后打印一些內容,則可以看到字符串輸出。
您必須將parent
對象傳遞給該對象。 為此,我們必須通過添加該參數來修改構造函數。
class CommandPopup(QInputDialog):
""" popup for a single-line command to be entered"""
def __init__(self, parent=None):
super().__init__(parent=parent)
[...]
def command_popup(self):
print("print")
command = CommandPopup(self)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.