我想从中获取文本,如何获取? 我已经安装了 tkniter、customtkinter,但不知道如何从中获取文本
entry1=customtkinter.CTkEntry(master=frame, width=220, placeholder_text='Username')
查看customtkinter的源代码,似乎您可以将
customtkinter.StringVar()
绑定到条目的textvariable
并在该变量上调用get()
# instantiate a StringVar to store the entry contents
entry1_var = customtkinter.StringVar(value='Test')
# instantiate the entry widget
entry1 = customtkinter.CTkEntry(
master=frame,
width=220,
placeholder_text='Username',
# bind the variable to this widget
textvariable=entry1_var,
)
entry1.pack()
print(entry1_var.get())
# => Test
但是,您应该也可以直接在
get()
小部件本身上调用CTkEntry
,而不必担心实例化/绑定到textvariable
entry1.get()
https://customtkinter.tomschimansky.com/documentation/widgets/entry/ 此链接将带您访问 customtkinter 文档。导航到 CTk Widgets 中的 CTk Entry,CTkEntry 的方法之一是 .get(),它返回输入字段中的字符串。
这是访问输入字段用户名和密码的值的代码。
import customtkinter
customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("blue")
root = customtkinter.CTk()
root.title("app")
root.geometry("500x350")
def login():
username = entry1.get()
password = entry2.get()
print(username)
print(password)
frame = customtkinter.CTkFrame(master=root)
frame.pack(pady=20, padx=60, fill="both", expand=True)
label = customtkinter.CTkLabel(master=frame, text="Login System",
font=("Roboto",24))
label.pack(pady=12,padx=10)
entry1 = customtkinter.CTkEntry(master=frame,
placeholder_text="Username")
entry1.pack(pady=12,padx=10)
entry2 = customtkinter.CTkEntry(master=frame,
placeholder_text="Password", show="*")
entry2.pack(pady=12,padx=10)
button = customtkinter.CTkButton(master=frame,text="Login",
command=login, fg_color = "blue")
button.pack(pady=12,padx=10)
root.mainloop()