entry组件创建:
from tkinter import * windows = Tk() windows.title("test") windows.configure(bg="blue") label1 = Label(windows, text="account") label1.grid(column=0, row=0) label2 = Label(windows, text="passwd") label2.grid(column=0, row=1) entry1 = Entry(windows) entry1.grid(column=1, row=0) entry2 = Entry(windows) entry2.grid(column=1, row=1) windows.mainloop()
效果如下:
清空输入框
delete方法
使用方法如下
from tkinter import * windows = Tk() windows.title("test") windows.configure(bg="blue") def delete(): entry1.delete(0, END) entry2.delete(0, END) label1 = Label(windows, text="account") label1.grid(column=0, row=0) label2 = Label(windows, text="passwd") label2.grid(column=0, row=1) entry1 = Entry(windows) entry1.grid(column=1, row=0) entry2 = Entry(windows) entry2.grid(column=1, row=1) button1 = Button(windows, text="按钮", command=delete) button1.grid(column=1, row=2, columnspan=2) windows.mainloop()
给按钮绑定函数中加入清空输入框方法,点击按钮后,即会清空输入框中的内容
获取输入框中的内容
有时候我们还需要获取输入框中的内容,比如获取用户输入的账号和密码与数据库中的账号密码对比
使用get方法实现
实现方法如下
from tkinter import * windows = Tk() windows.title("test") windows.configure(bg="blue") def delete(): print(entry1.get(), entry2.get()) label1 = Label(windows, text="account") label1.grid(column=0, row=0) label2 = Label(windows, text="passwd") label2.grid(column=0, row=1) entry1 = Entry(windows) entry1.grid(column=1, row=0) entry2 = Entry(windows) entry2.grid(column=1, row=1) button1 = Button(windows, text="按钮", command=delete) button1.grid(column=1, row=2, columnspan=2) windows.mainloop()
给按钮绑定打印输入框值的函数,按下后将打印输入框的值,如下图所示: