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

如何让Tkinter的StringVar获取CTkEntry输入的正确值?

问题解决:CTkEntry绑定StringVar后无法获取正确输入值

你的问题出在直接打印了StringVar对象本身,而不是它存储的输入内容。当你打印wishedLevel时,输出的是这个变量对象的标识符(比如"PY_VAR0"),而非输入框里的文本。

解决方法

要获取输入框的实际内容,需要调用StringVar的get()方法。修改你的faceIDValidation函数即可:

def faceIDValidation():
    print(wishedLevel.get())

修改后的完整代码

import tkinter 
import customtkinter

# System Settings ---------------------------------------------------------------------
customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("blue")

# App frame ---------------------------------------------------------------------------
app = customtkinter.CTk()
app.geometry("720x480")
app.title("Login to Database")

# Functions ---------------------------------------------------------------------------
def faceIDValidation():
    print(wishedLevel.get())  # 调用get()方法获取变量存储的文本值

#UI Elements --------------------------------------------------------------------------

text = customtkinter.CTkLabel(app, text="Insert here a number", font=customtkinter.CTkFont(size=15, weight="bold"))
text.pack(pady=10, padx=10)

wishedLevel = tkinter.StringVar()
databaseLevel = customtkinter.CTkEntry(app, placeholder_text="1, 2 or 3", width=100, textvariable=wishedLevel)
databaseLevel.pack(pady=10, padx=10)

camera = customtkinter.CTkCanvas(app)
camera.pack(pady=10, padx=10)

faceIdButton = customtkinter.CTkButton(app, text="Login", command=faceIDValidation)
faceIdButton.pack(pady=10, padx=10)

# Run app -----------------------------------------------------------------------------

app.mainloop()

修改后点击Login按钮,就能正确输出输入框内的内容了。

内容的提问来源于stack exchange,提问作者Vinícius Martins

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 07:14:58