[英]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.