Python tkinter (4) input box control


introduce

What about the excellent tkinter library and those controls? Let's take a look at the input box in this issue!

Create an input box

Make a window first, and then paste the input box on it.

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root) #创建一个输入框
entry.pack() #把输入框放到窗口上去
root.mainloop()

== Put ​​a picture!  ==

The properties of the input box

color

The input box can change its color. Colors include font color ( fgor foreground), background color ( bgor background), highlight color when selected ( selectbackground), and font color when selected ( selectforeground).

Colors can be represented by English words (such as "red", "blue"), or colors in HEX format (such as "#ff00ff", "#abcd00").

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root,fg="red",bg="blue",selectbackground="black",selectforeground="white")
#创建一个输入框,正常时蓝底红字,被选中的内容黑底白字
entry.pack() #把输入框放到窗口上去
root.mainloop()

insert image description here

font

You can also set the font of the input box. For example, I like Arial, and I want the font to be larger and italic.

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root,font=("宋体",25,"italic"))
#创建一个输入框,字体为宋体,字号25,斜体
#若不需要设置字体大小和字体样式,也可以直接写为:
#entry = tkinter.Entry(root,font="宋体")
entry.pack() #把输入框放到窗口上去
root.mainloop()

insert image description hereIn addition to italic font styles, there are the following types:

Key words style sample
roman normal characters insert image description here
italic italics insert image description here
bold Bold insert image description here
underline underline insert image description here
overstrike Bar off insert image description here

length

The input box can only set its length but not its width. Therefore, if you do something like this: entry1 = tkinter.Entry(root,width=50,height=20), you will get an error. The length is not the number of pixels, but the number of English letters that can be realized at one time. In other words, if the length is 50, about 50 English letters can be displayed at once without sliding left or right.

import tkinter
root = tkinter.Tk()
entry1 = tkinter.Entry(root,width=50) #长度约为50个英文字母
entry1.pack()
entry2 = tkinter.Entry(root,width=20) #长度约为20个英文字母
entry2.pack()
root.mainloop()

insert image description here

cursor

Cursors come in many styles. There is a lot of content, so let’s introduce it briefly here, and I will write a special article for the rest of the content when I find time.

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root,cursor="crosshair") #光标放在输入框上后样式为十字准心
entry.pack() #把输入框粘到窗口上
root.mainloop()

insert image description here
The default mouse style is xterm (text editor, I-shaped), in addition to some other mouse styles, such as arrow and so on.

state

General tkinter controls have two commonly used states: normal (Normal), disabled (Disabled), and some less commonly used ones, such as read-only (Readonly), active (Active), here only introduce Disabled and Normal, because The other two are just too uncommon.

import tkinter
root = tkinter.Tk()
entry1 = tkinter.Entry(root,state=tkinter.DISABLED)
#禁用,无法点击也无法往里面输入内容,也无法从中复制内容
entry1.pack() 
entry2 = tkinter.Entry(root,state=tkinter.NORMAL)
#可以进行操作
entry2.pack() 
entry3 = tkinter.Entry(root,state="readonly")
#只能复制内容,而不可以输入内容。
#tkinter模块中,没有tkinter.READONLY 这个变量,所有就用字符串代替吧
entry3.pack()
root.mainloop()

insert image description here

frame

reliefThere are several types of border styles ( ): flat, groove, raised, ridge, solid, sunken, let’s take a look at the effect together!

import tkinter
root = tkinter.Tk()
relief = ["flat","groove","raised","ridge","solid","sunken"] #不同的样式
for i in range(len(relief)):
    entry = tkinter.Entry(root,relief=relief[i]) #每个样式来一个输入框
    entry.place(x=5,y=i * 30) #把每个样式的输入框放在窗口上

root.mainloop()

insert image description here
With the border style, we can also set the thickness of the border: bdor borderwidth.

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root,bd=20) #厚厚的一层边框
entry.pack() #把输入框放在窗口上
root.mainloop()

insert image description here
The result is like this, the border is really touching!

displayed characters

If you want to create a password input box, then you definitely want the entered password to be displayed as stars or dots. The key lies in showthe attribute. If you want it to display stars, then show="*"you want it to display the original content, then show="".

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root,show="*") #将输入的内容显示为"*"
#当然,你也可以将星星替换成你喜欢的字符
#如果想让输入框显示原来的内容,则可以:
#entry.configure(show="")
entry.pack()
root.mainloop()

insert image description here

alignment

In WPS, there are three main ways of text layout: left, center, and right. In fact, Entry also has these three types, such as the example:

import tkinter
root = tkinter.Tk()
entry1 = tkinter.Entry(root,justify=tkinter.RIGHT) #靠右
entry1.pack()
entry2 = tkinter.Entry(root,justify=tkinter.LEFT) #靠左
entry2.pack()
entry3 = tkinter.Entry(root,justify=tkinter.CENTER) #居中
entry3.pack()
root.mainloop()

insert image description here

Cursor blinking and thickness

The blinking speed of the cursor can be changed using the properties insertontimei.e. how long the cursor is shown, and insertofftime.i.e. how long the cursor is hidden.

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root,insertofftime=100,insertontime=1000)
#输入的时候,每次光标闪烁,都显示1000毫秒(即1秒),隐藏100毫秒
entry.pack()
root.mainloop()

At the same time, you can also change the thickness of the cursor, that is insertwidththe property.

import tkinter
root = tkinter.Tk()
entry1 = tkinter.Entry(root,insertwidth=1) #细细的光标,粗细为1
entry1.pack()
entry2 = tkinter.Entry(root,insertwidth=10) #粗细为10
entry2.pack()
root.mainloop()

insert image description hereinsert image description here

modify properties

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root) #输入框
entry.pack() #把输入框贴在窗口上
entry.configure(font=("Consolas",50)) #将字体设置为Consolas,字体大小50,默认样式
#configure 和 config 都可以,
#entry.config(font=("Consolas",50)) 效果一样
root.mainloop()

method

In addition to giving various attributes to the input box, we also need to operate on it.

insert content

If we want to insert content inside the input box, we can use insert()method. details as follows:

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry()
entry.pack()
entry.insert(tkinter.END,"Hello World!!!")
#在输入框中文字的末尾添加字符串"Hello World!!!"
root.mainloop()

And besides tkinter.END, it can be tkinter.INSERT. The difference between the two is that tkinter.ENDit means to add a character string at the end of the text, while tkinter.INSERTit means to add a character string at the current cursor position.

get content

When we need to get the content in the input box, we can call get()the method.

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry()
entry.pack()
entry.insert(tkinter.END,"Hello World!!!") #插入文本
get_something = entry.get() #获取输入框中的内容
print(get_something)
root.mainloop()

The above program will output the content in the input box, here is of course "Hello World!!!".

delete content

In these kinds of operations, how can delete be missing! To delete the content in the input box, call delete()the method.

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root)
entry.pack()
entry.insert(tkinter.END,"Hello World!!!") #插入文本
entry.delete(0,tkinter.END) #删除全部内容
root.mainloop()

The usage is delete()a little more complicated, let's look at the following table:

example mean
entry.delete(0) delete the first character
entry.delete(1) delete the 2nd character
entry.delete(0,2) Delete the first 1-2 characters
entry.delete(0,tkinter.END) delete all characters
entry.delete(0,tkinter.INSERT) Delete from the first one to the previous character at the cursor position

move cursor

Move the cursor to the specified position, and look directly at the following chestnuts without further ado:

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root)
entry.pack()
entry.icursor(0) #将光标移动到第一个字符之前
root.mainloop()

Selected content

We can let the input box select a certain part of the content, which are respectively entry.select_from()and entry.select_to()methods, such as:

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root)
entry.insert(tkinter.END,'Hello World!!!!')
entry.select_from(0) #从第1个字符开始
entry.select_to(7) #一直选择到第8个字符
entry.pack()

But it can also be unchecked:

import tkinter
root = tkinter.Tk()
entry = tkinter.Entry(root)
entry.insert(tkinter.END,'Hello World!!!!')
entry.select_clear() #取消选中
entry.pack()

Summarize

The above is the main content of the input box. Let's review it together:

Attributes significance
fg or foreground font color
bg or background background color
selectforeground Font color when selected
selectbackground Background color when selected
font Font, you can pass in a tuple or a string
width length
cursor The style when the cursor is placed on the input box
state Input box state, such as disabled (Disabled), normal (Normal)
relief border style
bd or borderwidth border thickness
show How to display the entered content
insertontime The time displayed by the cursor
insertofftime time the cursor is hidden
insertwidth cursor thickness
import tkinter
root = tkinter.Tk()

entry = tkinter.Entry(root) #创建一个输入框

entry = tkinter.Entry(root,fg="red",bg="blue",selectbackground="black",selectforeground="white") #正常时蓝底红字,被选中的内容黑底白字
entry = tkinter.Entry(root,font=("宋体",25,"italic")) #字体为宋体,字号25,斜体
entry = tkinter.Entry(root,font="宋体") #字体为宋体,默认字体大小,默认样式

entry1 = tkinter.Entry(root,width=50) #长度约为50个英文字母
entry2 = tkinter.Entry(root,width=20) #长度约为20个英文字母

entry = tkinter.Entry(root,cursor="crosshair") #光标放在输入框上后样式为十字准心

entry1 = tkinter.Entry(root,state=tkinter.DISABLED) #禁用,无法点击也无法往里面输入内容,也无法从中复制内容
entry2 = tkinter.Entry(root,state=tkinter.NORMAL) #可以进行操作
entry3 = tkinter.Entry(root,state="readonly") #只能复制内容,而不可以输入内容。tkinter模块中,没有tkinter.READONLY 这个变量,所有就用字符串代替吧

entry = tkinter.Entry(root,relief="groove") #不同边框样式的输入框
entry = tkinter.Entry(root,bd=20) #厚厚的一层边框

entry = tkinter.Entry(root,show="*") #将输入的内容显示为"*"
entry = tkinter.Entry(root,show="") #显示原来的内容

entry1 = tkinter.Entry(root,justify=tkinter.RIGHT) #靠右
entry2 = tkinter.Entry(root,justify=tkinter.LEFT) #靠左
entry3 = tkinter.Entry(root,justify=tkinter.CENTER) #居中

entry = tkinter.Entry(root,insertofftime=100,insertontime=1000) #输入的时候,每次光标闪烁,都显示1000毫秒(即1秒),隐藏100毫秒

entry1 = tkinter.Entry(root,insertwidth=1) #细细的光标,粗细为1
entry2 = tkinter.Entry(root,insertwidth=10) #粗细为10

entry.configure(font=("Consolas",50)) #将字体更换为Consolas,字号50,默认样式

entry.insert(tkinter.END,"Hello World!!!") #在输入框中文字的末尾添加字符串"Hello World!!!"
entry.delete(0,tkinter.END) #删除全部内容
entry.get() #获取输入框中的内容

entry.icursor(0) #将光标移动到第一个字符之前

entry.pack() #把输入框放到窗口上去


entry.select_from(0) #从第1个字符开始选择
entry.select_to(7) #一直选择到第8个字符

entry.select_clear() #取消选中

root.mainloop()

Haha, thanks for reading! Like it, pay attention to it! See you next time~

Guess you like

Origin blog.csdn.net/jzwalliser/article/details/128762107