Python tkinter library Entry control Text control

EntryThe control is used to input text (single line input control)
Entry. The showkeyword parameter of the construction method specifies that the input text is to echo a character
Textcontrol is used to input text (multi-line text, image, rich text, etc.)

The following examples are Entrycontrols Textcontrols

EntryControls
When text is entered in the first Entry control, * will be echoed, and
the second one is to display the original text
Textcontrol of the first entry.
Insert (image, text, image)

from tkinter import *#导入tkinter
window = Tk()#创建Tk实例,也就是要显示的窗口
window.title("dalaojun")#窗口标题
window["background"]="#152950"#窗口背景颜色
window.geometry("600x600+30+30")#窗口大小以及距离屏幕桌上角的坐标
entryVar1 = StringVar() #该变量绑定了第1个Entry控件

# 在第1个Entry控件中输入文本时回调的函数    更新第2个Entry控件中的文本
def callback():
    entryVar2.set(entryVar1.get())
#将第1个Entry控件与entryVar1绑定
# w表示写入时调用callback
# a,b,c 是lambda表达式要求传入的3个参数,(本实例用不到这3个参数,但是必须要指定否则抛出异常)
entryVar1.trace("w", lambda a,b,c: callback())
entry1 =Entry(window,textvariable=entryVar1,show="*") #Entry控件是用来输入文本的(单行输入控件)  show关键字参数回显*号
entry1.pack(pady=10)#对该Entry控件使用pack布局,垂直外边距为10
entryVar2 = StringVar()#创建第2个Entry控件
entry2 =Entry(window,textvariable=entryVar2)
entry2.pack(pady=10)#对该Entry控件使用pack布局,垂直外边距为10
text =Text(window)#创建Text控件 Text控件是用来输入文本的(多行文本,图像,富文本等)
text.pack(pady=10)#对该Text控件使用pack布局,垂直外边距为10
from PIL import Image, ImageTk
#Text控件只支持插入少数图像格式(gif,bmp,等)
# 导入PIL库使本脚本支持插入jpg,png格式图像
pic =Image.open("pic.png")# 打开图片   (与本脚本文件在相同路径的有一个pic.png文件的图片)
photo1 =ImageTk.PhotoImage(pic)
text.image_create(END,image=photo1)#在Text控件末尾添加图像
text.tag_configure("big",font=("Arial",25,"bold"))#在Text控件的结尾插入文本,并使用big指定的字体属性
text.insert(END,"五六七","big")#需要插入的文本,以及字体属性
ha =Image.open("ha.jpg")#再打开图片 (与本脚本文件在相同路径的有一个ha.jpg文件的图片)
photo2=ImageTk.PhotoImage(ha)
text.image_create(END,image=photo2)#在Text控件末尾添加图像
mainloop()#调用mainloop函数进入事件循环

Guess you like

Origin blog.csdn.net/weixin_47021806/article/details/115255906