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

Python类中customtkinter按钮调用start()失效及启动自动执行问题

CustomTkinter应用启动自动执行函数与按钮点击无响应问题解决

问题核心原因

  • 启动时自动调用start():按钮的command参数直接执行了函数(如command=self.start(self, tokens.get(...))),程序初始化按钮时就会触发函数执行,而非绑定回调引用。
  • 按钮点击无响应:command接收的是函数执行后的返回值(此处为None),而非可调用的函数对象,导致点击无触发逻辑。
  • 类初始化方式错误:直接调用application.__init__(self=application)不符合Python类实例化规范。
  • 方法调用冗余:ui和run方法调用时额外传递self,属于错误用法。

修正后的代码

import random
import customtkinter

customtkinter.set_appearance_mode('dark')
customtkinter.set_default_color_theme('green')
app = customtkinter.CTk()

app.geometry('600x500')
app.title("app")

quotes = [""]

class Application:
    def __init__(self):
        self.tokens_textbox = None
        self.ui()
        self.run()
    
    def start(self):
        tok = self.tokens_textbox.get("1.0", "end-1c")
        print("executed")
        print(tok)
    
    def oF(self):
        return
    
    def sF(self):
        return

    def ui(self):
        title = customtkinter.CTkLabel(master=app, text="App", font=("", 24))
        quote = customtkinter.CTkLabel(master=app, text=random.choice(quotes), font=("", 16))

        slb = customtkinter.CTkLabel(master=app, text="Server ID")
        clb = customtkinter.CTkLabel(master=app, text="Chat ID")
        sid = customtkinter.CTkEntry(master=app, width=148, height=28)
        cid = customtkinter.CTkEntry(master=app, width=148, height=28)

        tkt = customtkinter.CTkLabel(master=app, text="Tokens", font=("", 20))
        self.tokens_textbox = customtkinter.CTkTextbox(master=app, width=324, height=200)

        but1 = customtkinter.CTkButton(
            master=app, 
            width=96, 
            height=24, 
            text="Start", 
            command=self.start
        )
        openf = customtkinter.CTkButton(
            master=app, 
            width=96, 
            height=24, 
            text="Open", 
            command=self.oF
        )
        savef = customtkinter.CTkButton(
            master=app, 
            width=96, 
            height=24, 
            text="Save", 
            command=self.sF
        )

        but1.place(relx=0.5, rely=0.4, anchor=customtkinter.CENTER)
        savef.place(relx=0.4, rely=0.95, anchor=customtkinter.CENTER)
        openf.place(relx=0.6, rely=0.95, anchor=customtkinter.CENTER)
        sid.place(relx=0.5, rely=0.2, anchor=customtkinter.CENTER)
        cid.place(relx=0.5, rely=0.3, anchor=customtkinter.CENTER)
        slb.place(relx=0.3, rely=0.2, anchor=customtkinter.CENTER)
        clb.place(relx=0.3, rely=0.3, anchor=customtkinter.CENTER)
        tkt.place(relx=0.5, rely=0.475, anchor=customtkinter.CENTER)
        self.tokens_textbox.place(relx=0.5, rely=0.7, anchor=customtkinter.CENTER)

        title.place(relx=0.5, rely=0.075, anchor=customtkinter.CENTER)
        quote.place(relx=0.5, rely=0.125, anchor=customtkinter.CENTER)

    def run(self):
        app.mainloop()

if __name__ == "__main__":
    app_instance = Application()

关键修改点

  1. 按钮回调绑定:将command=self.start(self, ...)改为command=self.start,直接传递函数引用,避免启动时触发执行。若需传参,可使用lambda: self.start(...),但优先将组件设为实例属性,在回调内获取值更稳妥。
  2. 类实例化:用app_instance = Application()替代错误的初始化方式,符合Python类使用规范。
  3. 实例属性存储组件:将文本框设为self.tokens_textbox,让start()方法在点击时动态获取内容,避免提前获取空值。
  4. 方法调用修正:__init__内调用self.ui()和self.run(),不再额外传递self。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 21:16:08