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

CustomTkinter CTkTextbox无法运行,提示CTkFont参数不被支持

问题解决:CustomTkinter计算器CTkTextbox报错及代码修复

核心错误原因

你遇到的ValueError: ['CTkFont'] are not supported arguments是因为CTkTextbox和CTkButton的字体参数名是font,而非CTkFont,字体需要通过customtkinter.CTkFont()创建对象,或者直接传入字体名称+大小的元组。

其他代码问题及修复

除了字体参数错误,代码里还有几个关键问题:

  • 类构造函数名写错:__int__应该是__init__,否则类实例化时不会执行初始化逻辑
  • 错误覆盖了App类:App = customtkinter.CTkFrame会把你定义的App类替换成CTkFrame,导致窗口无法正常创建
  • 全局变量使用不当:用类属性替代全局变量更符合面向对象规范
  • CTkTextbox设置state="disabled"后,无法直接修改内容,需要临时切换为normal完成编辑后再改回disabled

完整修复后的代码

import customtkinter

customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("blue")


class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        self.title("Cal")
        self.geometry("265x280")
        self.cal_prb = ""

        # 创建字体对象
        text_font = customtkinter.CTkFont(family="Roboto", size=24)
        btn_font = customtkinter.CTkFont(family="Roboto", size=14)

        # 文本框
        self.textbox = customtkinter.CTkTextbox(self, width=250, height=50, font=text_font)
        self.textbox.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
        self.textbox.configure(state="disabled")

        # 按钮布局
        btn_open = customtkinter.CTkButton(self, text="(", command=lambda: self.add_to_prb("("), width=60, font=btn_font)
        btn_open.grid(row=1, column=0, padx=5, pady=5)
        btn_close = customtkinter.CTkButton(self, text=")", command=lambda: self.add_to_prb(")"), width=60, font=btn_font)
        btn_close.grid(row=1, column=1, padx=5, pady=5)
        btn_dot = customtkinter.CTkButton(self, text=".", command=lambda: self.add_to_prb("."), width=60, font=btn_font)
        btn_dot.grid(row=1, column=2, padx=5, pady=5)
        btn_percent = customtkinter.CTkButton(self, text="%", command=lambda: self.add_to_prb("%"), width=60, font=btn_font)
        btn_percent.grid(row=1, column=3, padx=5, pady=5)

        btn_7 = customtkinter.CTkButton(self, text="7", command=lambda: self.add_to_prb(7), width=60, font=btn_font)
        btn_7.grid(row=2, column=0, padx=5, pady=5)
        btn_8 = customtkinter.CTkButton(self, text="8", command=lambda: self.add_to_prb(8), width=60, font=btn_font)
        btn_8.grid(row=2, column=1, padx=5, pady=5)
        btn_9 = customtkinter.CTkButton(self, text="9", command=lambda: self.add_to_prb(9), width=60, font=btn_font)
        btn_9.grid(row=2, column=2, padx=5, pady=5)
        btn_times = customtkinter.CTkButton(self, text="X", command=lambda: self.add_to_prb("*"), width=60, font=btn_font)
        btn_times.grid(row=2, column=3, padx=5, pady=5)

        btn_4 = customtkinter.CTkButton(self, text="4", command=lambda: self.add_to_prb(4), width=60, font=btn_font)
        btn_4.grid(row=3, column=0, padx=5, pady=5)
        btn_5 = customtkinter.CTkButton(self, text="5", command=lambda: self.add_to_prb(5), width=60, font=btn_font)
        btn_5.grid(row=3, column=1, padx=5, pady=5)
        btn_6 = customtkinter.CTkButton(self, text="6", command=lambda: self.add_to_prb(6), width=60, font=btn_font)
        btn_6.grid(row=3, column=2, padx=5, pady=5)
        btn_minus = customtkinter.CTkButton(self, text="-", command=lambda: self.add_to_prb("-"), width=60, font=btn_font)
        btn_minus.grid(row=3, column=3, padx=5, pady=5)

        btn_1 = customtkinter.CTkButton(self, text="1", command=lambda: self.add_to_prb(1), width=60, font=btn_font)
        btn_1.grid(row=4, column=0, padx=5, pady=5)
        btn_2 = customtkinter.CTkButton(self, text="2", command=lambda: self.add_to_prb(2), width=60, font=btn_font)
        btn_2.grid(row=4, column=1, padx=5, pady=5)
        btn_3 = customtkinter.CTkButton(self, text="3", command=lambda: self.add_to_prb(3), width=60, font=btn_font)
        btn_3.grid(row=4, column=2, padx=5, pady=5)
        btn_plus = customtkinter.CTkButton(self, text="+", command=lambda: self.add_to_prb("+"), width=60, font=btn_font)
        btn_plus.grid(row=4, column=3, padx=5, pady=5)

        btn_0 = customtkinter.CTkButton(self, text="0", command=lambda: self.add_to_prb(0), width=60, font=btn_font)
        btn_0.grid(row=5, column=0, padx=5, pady=5)
        btn_clr = customtkinter.CTkButton(self, text="CLR", command=self.clr_prb, width=60, font=btn_font)
        btn_clr.grid(row=5, column=1, padx=5, pady=5)
        btn_slv = customtkinter.CTkButton(self, text="=", command=self.slv_prb, width=60, font=btn_font)
        btn_slv.grid(row=5, column=2, padx=5, pady=5)
        btn_div = customtkinter.CTkButton(self, text="/", command=lambda: self.add_to_prb("/"), width=60, font=btn_font)
        btn_div.grid(row=5, column=3, padx=5, pady=5)

    def add_to_prb(self, symbol):
        self.cal_prb += str(symbol)
        self.textbox.configure(state="normal")
        self.textbox.delete(1.0, "end")
        self.textbox.insert(1.0, self.cal_prb)
        self.textbox.configure(state="disabled")

    def slv_prb(self):
        try:
            # 处理%:比如10%转为0.1
            prb = self.cal_prb.replace("%", "/100")
            self.cal_prb = str(eval(prb))
            self.textbox.configure(state="normal")
            self.textbox.delete(1.0, "end")
            self.textbox.insert(1.0, self.cal_prb)
            self.textbox.configure(state="disabled")
        except:
            self.clr_prb()
            self.textbox.configure(state="normal")
            self.textbox.insert(1.0, "Error")
            self.textbox.configure(state="disabled")

    def clr_prb(self):
        self.cal_prb = ""
        self.textbox.configure(state="normal")
        self.textbox.delete(1.0, "end")
        self.textbox.configure(state="disabled")


if __name__ == "__main__":
    app = App()
    app.mainloop()

关键修复点说明

  1. 字体参数修正:将所有CTkFont参数改为font,并统一创建CTkFont对象传入,也可以直接用font=("Roboto", 24)这种元组形式
  2. 类构造函数修正:把__int__改为__init__,确保窗口初始化逻辑正常执行
  3. 移除错误的类覆盖:删除App = customtkinter.CTkFrame,实例化自己定义的App类
  4. 文本框状态切换:修改内容前先设置state="normal",完成后恢复state="disabled"
  5. 面向对象优化:用类属性self.cal_prb替代全局变量,避免全局变量带来的问题
  6. 按钮布局优化:调整了按钮宽度和间距,让界面更美观

内容的提问来源于stack exchange,提问作者Poker Cat

相关产品推荐
方舟 Agent Plan

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

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