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

点击Save按钮后无法编辑CustomTkinter Entry输入框求助

问题描述

点击Save按钮后会弹出消息框,随后输入框内的占位符被删除,但关闭消息框后,应用中的两个Entry输入框无法编辑。尝试修改state属性和使用focus.set()方法,均未解决问题。

相关代码

submitTask方法

def submitTask(self):
        title = self.taskTitle.get()
        summary = self.taskSummary.get()
        # checks if summary outputs the placeholder
        if summary == "Task Summary...":
            summary = ""

        # checks if the title is empty or has the placeholder
        if title == "Task Title..." or title == "":
            msg1 = CTkMessagebox(
                title="Message",
                message="Please enter in a Task Title.",
                justify="center",
            )
        

        else:
            msg2 = CTkMessagebox(
                title="Message",
                message=f"Title: {title}\nSummary: {summary}",
                justify="center",
            )

        self.taskTitle.delete(0, "end")
        self.taskSummary.delete(0, "end")

createWidgets方法

def createWidgets(self):
        # task title entry box
        self.taskTitle = CTkEntry(
            self,
            placeholder_text="Task Title...",
            width=1000,
            height=65,
            font=(
                "Helvetica",
                35,
            ),
            placeholder_text_color="white",
            text_color="white",
        )
        self.taskTitle.pack(padx=50, pady=(20, 35))

        # task summary entry box
        self.taskSummary = CTkEntry(
            self,
            placeholder_text="Task Summary...",
            width=1000,
            height=65,
            font=("Helvetica", 35),
            placeholder_text_color="white",
            text_color="white",
        )
        self.taskSummary.pack(padx=50, pady=(10, 25))

        # back button
        self.backButton = CTkButton(
            self,
            text="ᐊ BACK",
            fg_color="#ed5e63",
            text_color="black",
            hover_color="#f28c8f",
            border_color="black",
            border_width=2.2,
            corner_radius=16,
            command=self.confirmChanges,
            width=160,
            height=45,
        )
        self.backButton.pack(side="left", anchor="s", padx=(20, 10), pady=30)
        self.backButton.configure(font=("Helvetica", 15))

        # save button
        self.saveButton = CTkButton(
            self,
            text="SAVE",
            fg_color="#ed5e63",
            text_color="black",
            hover_color="#f28c8f",
            border_color="black",
            border_width=2.2,
            corner_radius=16,
            command=self.submitTask,
            width=160,
            height=45,
        )
        self.saveButton.pack(side="right", anchor="s", padx=(10, 20), pady=30)
        self.saveButton.configure(font=("Helvetica", 15))
解决方案

问题根源是创建CTkMessagebox后,未等待用户关闭消息框就直接执行输入框内容删除操作,导致输入框焦点和状态异常。只需在每个消息框创建后调用wait_window()方法,等待消息框关闭再执行后续操作即可:

修改后的submitTask方法:

def submitTask(self):
        title = self.taskTitle.get()
        summary = self.taskSummary.get()
        # checks if summary outputs the placeholder
        if summary == "Task Summary...":
            summary = ""

        # checks if the title is empty or has the placeholder
        if title == "Task Title..." or title == "":
            msg1 = CTkMessagebox(
                title="Message",
                message="Please enter in a Task Title.",
                justify="center",
            )
            msg1.wait_window()  # 等待消息框关闭
        else:
            msg2 = CTkMessagebox(
                title="Message",
                message=f"Title: {title}\nSummary: {summary}",
                justify="center",
            )
            msg2.wait_window()  # 等待消息框关闭

        self.taskTitle.delete(0, "end")
        self.taskSummary.delete(0, "end")
        # 可选:将焦点重新设置到标题输入框,提升用户体验
        self.taskTitle.focus_set()

wait_window()会阻塞代码执行,直到消息框被关闭,确保输入框操作在消息框完全关闭后进行,避免状态冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 06:02:16