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

Mac平台Python应用点击按钮触发Segmentation Fault故障求助

解决Mac平台CustomTkinter弹窗触发Segmentation Fault的问题

核心排查方向

Segmentation Fault仅在Mac出现、Windows正常,且报错指向Tkinter主循环,大概率是CustomTkinter组件与Mac系统Tk环境的兼容性问题,或是循环创建组件时的底层渲染冲突。结合代码可从以下几点入手:


1. 替换只读输入框为标签(最可能解决问题)

你的代码用CTkEntry展示药品数据但未开放编辑功能,改用CTkLabel可避免Entry组件在Mac上的内存渲染异常:

def place_order(self):
    """Pop up a window to confirm the order. Displays the order list."""

    self.order_confirmation = ctk.CTkToplevel(self.root)
    self.order_confirmation.title("Order Confirmation")
    self.order_confirmation.resizable(False, False)

    if not self.order_list:
        order_confirmation_label = ctk.CTkLabel(
            self.order_confirmation,
            text="No medicines selected.",
            font=self.text_font,
        )
        order_confirmation_label.pack(padx=20, pady=20, anchor=ctk.CENTER)

        close_button = ctk.CTkButton(
            self.order_confirmation,
            text="Close Window",
            command=self.order_confirmation.destroy,
        )
        close_button.pack(pady=20)

    else:
        order_confirmation_label = ctk.CTkLabel(
            self.order_confirmation,
            text="The following medicines have been selected:",
            font=self.text_font,
        )
        order_confirmation_label.pack(padx=20, pady=20, anchor=ctk.CENTER)

        order_list_frame = ctk.CTkFrame(self.order_confirmation)

        row = 0
        total_amount = 0
        for i in self.dataset:
            if i[0] in self.order_list:
                for j in range(0, len(i) - 1):
                    # 替换CTkEntry为CTkLabel
                    order_cell = ctk.CTkLabel(
                        order_list_frame,
                        width=self.column_widths[j],
                        font=self.text_font,
                        text=i[j].capitalize() if hasattr(i[j], 'capitalize') else i[j]
                    )
                    order_cell.grid(
                        row=row,
                        column=(j + 1),
                        pady=5,
                        ipady=1,
                        padx=5,
                    )
                row += 1
                total_amount += i[-2]

        order_list_frame.pack(padx=20, pady=20, anchor=ctk.CENTER)

        self.final_confirmation_button = ctk.CTkButton(
            self.order_confirmation,
            text="Confirm Order",
            font=self.text_font,
            command=self.final_confirm_button_pressed,
            corner_radius=10,
            height=40,
        )

        total_amount_label = ctk.CTkLabel(
            self.order_confirmation,
            text=f"Number of medicines ordered: {row}\nTotal Amount: {total_amount}",
            font=self.text_font,
        )

        total_amount_label.pack(padx=20, pady=20, anchor=ctk.CENTER)
        self.final_confirmation_button.pack(padx=20, pady=20, anchor=ctk.CENTER)

2. 其他排查修复步骤

  • 版本兼容性检查:Python 3.11与部分旧版CustomTkinter存在兼容问题,升级到最新稳定版:
    pip install --upgrade customtkinter
    
  • 验证索引安全性:确保self.column_widths的长度等于len(i)-1,避免索引越界触发底层内存异常
  • 优化组件创建逻辑:循环创建组件时,先批量创建再统一布局,或限制单次创建的组件数量(Mac的Tk对大量组件的渲染压力更敏感)
  • 更换Python环境:pyenv管理的Python可能自带旧版Tk,尝试使用Mac系统自带Python,或重新编译pyenv的Python并启用最新Tk支持

3. 错误日志分析补充

报错指向mainloop()是因为Segmentation Fault发生在Tk的底层渲染线程,Python层无法捕获具体组件的错误,因此需要从组件使用和环境兼容性入手排查。

内容的提问来源于stack exchange,提问作者Chirag Aggarwal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 13:20:28