You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PySide6按钮点击无响应求助(点击事件无终端输出)

PySide6按钮点击无响应(终端无打印输出)的解决方案

核心原因:临时实例被垃圾回收

你在MainWindow的customerUI方法里创建CustomerWidget实例时,没有将它保存为类的属性:

def customerUI(self):
    CustomerWidget(self.customer_widget)

Python的垃圾回收机制会自动清理没有被任何变量引用的对象,这就导致CustomerWidget里的按钮信号连接被销毁,点击按钮自然不会触发预设的打印逻辑。

修复步骤

修改MainWindow中的customerUI方法,把CustomerWidget实例保存为类属性,确保它不会被回收:

def customerUI(self):
    self.customer_widget_instance = CustomerWidget(self.customer_widget)

这样实例会被MainWindow对象持有,信号连接就能正常生效,点击saveButton后终端会输出预设内容。

额外调试建议

  1. 确认Ui_CustomerWidget里的按钮objectName确实是saveButton,和代码中绑定的名称一致。
  2. 在CustomerWidget的__init__方法中添加打印语句,验证实例是否被正确创建:
    def __init__(self, stack=None):
        super().__init__()
        print("CustomerWidget 实例初始化完成")
        self.customer_widget_ui = Ui_CustomerWidget()
        self.customer_widget_ui.setupUi(stack)
        self.customer_widget_ui.saveButton.clicked.connect(self.testButton)
    
  3. 检查customer_widget是否被正确添加到QStackedWidget,且切换到该页面时处于可见状态。

内容的提问来源于stack exchange,提问作者Muhammad Irgi Hakim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 09:58:31