Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Entry in tkinter

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use Entry in tkinter. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

A text box Entry is usually a single-line text box, using the following format:

Entry (parent, options,...)

Common options parameters:

Bg or background: background color.

Borderwidth or bd: width of the boundary. Default is 2 pixels.

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

Cursor: the shape when the mouse is over the check box.

Exportselection: if you perform a selection, 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 box. The default is relief=FLAT.

Selectbackground: the background color of the selected string.

Selectborderwidth: the width of the boundary when selecting a string. The default is 1.

Selectforeground: the foreground color of the selected string.

Show: displays the entered string, and show='*' indicates that the asterisk is displayed, which is often used to enter the password field.

State: input status. The default is NORMAL, which means it can be entered, and DISABLE means it cannot be entered.

Textvariable: text variable.

Width: wide, default is characters.

Xscrollcommand: use scroll bars on the x-axis.

36 A simple example:

From tkinter import * win=Tk () label1=Label (win,text= "username") 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. Further landing cases:

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= "login", command=printview) login.grid (row=2,column=0) exit=Button (win Text= "exit") exit.grid (row=2,column=1) win.mainloop ()

The insert method of Entry. In the Entry control, you can use the insert (index,s) method to insert a string. S is the inserted string, and the string is 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 ()

The delete method of Entry, delete (first,last=None) deletes the string from the first character to the first-1 character within the Entry, and uses delete (zero end) if you want 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 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 () Thank you for reading! This is the end of the article on "how to use Entry in tkinter". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report