04-tkinter's Entry Notes

Keywords: Python Asterisk

Text box Entry is usually a single-line text box in the following format:
Entry (parent object, options,...)

Common options parameters:

bg or background: Background color.

borderwidth or bd: The boundary width, which defaults to 2 pixels.

command: this function is automatically executed when the user changes the content.

cursor: The shape of the mouse when it is on the check box.

Exportselection: When a selection is performed, the selected characters are automatically output to the clipboard if you want to avoid exportselection=0.

fg: foreground color.

font: glyph.

height: high, in characters.

highlightbackground: The background color when the text box gets focus.

highlightcolor: The color when the text box gets focus.

justify: The alignment of the last line when there are multiple lines of text.

Relief: controls the text outline, relief=FLAT by default

selectbackground: The background color of the selected string.

selectborderwidth: The boundary width when a string is selected, preset to be 1.

selectforeground: The foreground color of the selected string.

Show: Displays the input string, show='*'indicates the display asterisk, commonly used to enter a password field.

State: The input state, NORMAL by default, means it can be entered, DISABLE means it cannot be entered.

textvariable: Text variable.

width: wide, default is character.

xscrollcommand: Use scrollbars on the x-axis.

36 A simple example:

from tkinter import *
win=Tk()
label1=Label(win,text="User name")
label2=Label(win,text="Password")
name=Entry(win)
passwd=Entry(win,show="*")
label1.grid(row=0,column=0)
label2.grid(row=1,column=0)
name.grid(row=0,column=1)
passwd.grid(row=1,column=1)
win.mainloop()

There is a get() method in Entry that can be used to get the contents of the current Entry string.

37. Landing cases further:

from tkinter import *
win=Tk()
def printview():
    print("Name :%s\npasswd:%s"%(name.get(),passwd.get()))

label1=Label(win,text="Name")
label1.grid(row=0,column=0)
label2=Label(win,text="passwd")
label2.grid(row=1,column=0)
name=Entry(win)
name.grid(row=0,column=1)
passwd=Entry(win,show="*")
passwd.grid(row=1,column=1)
login=Button(win,text="Land",command=printview)
login.grid(row=2,column=0)
exit=Button(win,text="Sign out")
exit.grid(row=2,column=1)
win.mainloop()

Entry's insert method, in the Entry control you can use insert(index,s) method to insert a string, s is the inserted string, the string will be inserted in the index position;

38 Use of insert:

from tkinter import *
win=Tk()
label1=Label(win,text="Name")
label1.grid(row=0,column=0)
label2=Label(win,text="passwd")
label2.grid(row=1,column=0)
name=Entry(win)
name.grid(row=0,column=1)
passwd=Entry(win,show="*")
passwd.grid(row=1,column=1)
name.insert(0,"john")
passwd.insert(0,"123456")
win.mainloop()

Entry's delete method, delete(first,last=None), deletes a string from first character to first-1 character in Entry, if delete(0,END) is used to delete the entire string.

39 Use of delete

from tkinter import *
win=Tk()
def showview():
print("name:%s\npasswd:%s"%(name.get(),passwd.get()))
name.delete(0,END)
passwd.delete(0,END)

label1=Label(win,text="name")
label1.grid(row=0,column=0)
label2=Label(win,text="passwd")
label2.grid(row=1,column=0)
name=Entry(win)
name.grid(row=0,column=1)
passwd=Entry(win,show="*")
passwd.grid(row=1,column=1)
button1=Button(win,text="login",command=showview)
button1.grid(row=2,column=0)
button2=Button(win,text="quit",command=win.quit)
button2.grid(row=2,column=1)
win.mainloop()

Calculate the mathematical expression eval;

Use of 40 eval:

from tkinter import *
win=Tk()
def showview():
    out.configure(text="Result"+str(eval(equ.get())))

label=Label(win,text="Please enter an expression")
label.pack()
equ=Entry(win)
equ.pack(pady=5)
out=Label(win)
out.pack()
button=Button(win,text="Calculation",command=showview)
button.pack()
win.mainloop()

Posted by webbnino on Sat, 16 Nov 2019 18:12:40 -0800