tkinter学习-文本框


阅读目录

  • Entry 输入框
  • Text 文本框

Entry:

  说明:输入控件,用于显示简单的文本内容

  属性:在输入框中用代码添加和删除内容,同样也是用insert()和delete()方法

 

from tkinter import * root = Tk() e = Entry(root) e.pack(padx=10,pady=10) #x,y轴的边距为10
e.insert(1,'')     #第一个参数是插入的位置,
e.insert(0,'') mainloop()

 结果:

    

  获取输入框里的内容,可以使用Entry组件的get()方法,通常是设置tkinter的变量(一般用StringVar)挂钩到textvatiable选项,然后再通过get()方法来获取

from tkinter import * root = Tk() root.title('山丘') root.geometry('300x160') Label(root,text='账号:').place(x=30,y=30) Label(root,text='密码:').place(x=30,y=70) v1 = StringVar() v2 = StringVar() e1 = Entry(root,textvariable=v1) e2 = Entry(root,textvariable=v2,show='*')   #用*号代替用户输入的内容
e1.place(x=80,y=30) e2.place(x=80,y=70) def show(): print('账号:%s' % v1.get()) print('密码:%s' % v2.get()) e1.delete(0,END) #获取完信息,清楚掉输入框的
    e2.delete(0,END)    #0,END,表示从第0个到最后一个
Button(root,text='获取信息',width=10,command=show).place(x=20,y=120) Button(root,text='退出',width=10,command=root.quit).place(x=180,y=120) mainloop()

 

结果:

    

  另外,Entry组件还支持验证输入的内容的合法性,例如输入框要求输入数字,用户输入的是字母那就是属于违法了,实现该功能,必须设置的validate,validatecommand,invalidcommand三个选项。

  首先启用验证的开关是validate选项,该选项的值(常用的):

  'focus':当Entry组件获得或失去焦点的时候

  'focusin':当Entry组件获得焦点的时候

  'focusout':当Entry组件失去焦点的时候

  'key':当输入框被编辑的时候

  其次是为validatecommand选项指定一个验证函数,该函数只能返回True或False表示验证结果,一般情况下验证函数只需要知道输入框的内容即可,

可以通过Entry组件的get()方法来获取这个字符串。

  最后,invalidcommand选项指定的函数只有在validatecommand的返回值为False的时候才被调用。

 

from tkinter import * root = Tk() def test(): if e1.get() == '山丘': print('正确') return True else: print('错误') return False def test1(): print('又错了') return True v = StringVar() e1=Entry(root,textvariable=v,validate='focusout',validatecommand=test,invalidcommand=test1) e2 = Entry(root) e1.pack(padx=10,pady=5) e2.pack(padx=10,pady=5) mainloop()

结果:

    

  其实,还有一个隐藏的功能,为验证函数提供一些额外的选项(常用的):

  ‘%P’:改值为输入框的最新文本内容

  为了使用这些选项,你可以这么写:validatecommand = (f,s1,s2,...)

  其中,f 是验证函数名,s1,s2,是额外的选项,这些选项为作为参数依次的传给f 函数,在此之前,需要调用 register()方法将验证函数包装起来。

 

from tkinter import * root = Tk() root.title('山丘') root.geometry('400x150') v1 = StringVar() v2 = StringVar() v3 = StringVar() def show(content): #注意,这里你不能用e1.get()来获取输入的内容,因为validate选项指定为'key' #这个时候有任何输入操作都会拦截到这个函数中,也就是说先拦截,只有这个函数 #返回的结果为True的时候,那么输入框的内容才会到变量里,所以要使用 #"%P"来获取最新的输入框的内容
    if content.isdigit(): return True else: return False show_com = root.register(show) e1 = Entry(root,textvariable=v1,width=10,validate='key',validatecommand=(show_com,'%P')).place(x=20,y=30) Label(root,text='x').place(x=125,y=30) e2 = Entry(root,textvariable=v2,width=10,validate='key',validatecommand=(show_com,'%P')).place(x=150,y=30) Label(root,text='=').place(x=250,y=30) e3 = Entry(root,textvariable=v3,width=10).place(x=280,y=30) def calc(): result = int(v1.get()) * int(v2.get()) v3.set(result) Button(root,text='计算结果',width=10,command=calc).place(x=150,y=90) mainloop()

结果:

    

Text:

  说明:文本控件,用于显示多行文本

 

from tkinter import * root = Tk() text = Text(root,width=30,height=5) text.pack() text.insert(INSERT,'想得却不可得\n')#INSERT索引表示光标当前的位置
text.insert(END,'你奈人生何') mainloop()

结果:

      

  当然,也可以插入image对象,

 

from tkinter import * root = Tk() text = Text(root) text.pack() photo = PhotoImage(file='8.gif') def show(): print('该舍却舍不得,只顾着跟往事瞎扯') b1 = Button(text,text='唱吧',command=show).place(x=0,y=0) def show1(): text.image_create(END,image=photo) b2 = Button(text,text='看吧',command=show1).place(x=0,y=35) text.window_create(INSERT,window=b1) text.window_create(END,window=b2) mainloop()

结果:

    

  Indexes用法:这个索引是用来指向组件中文本的位置,常用的索引类型:

  'line.column':用行号和列号组成的,注意,行号用1开始,列号用0开始。

  'line.end':行号加上‘.end’的格式表示该行的最后一个字符的位置

 

from tkinter import * root = Tk() text = Text(root,width=50,height=5) text.pack() text.insert(INSERT,'i love you') print(text.get('1.2',1.6)) print(text.get(1.2,'1.end')) mainloop()

结果:

    

  Marks的用法:通常是嵌套在Text组件的文本中的不可见对象

  用法:mark_set()方法创建,mark_unset()方法删除

 

from tkinter import * root = Tk() text = Text(root,width=20,height=5) text.insert(INSERT,'i love you') text.pack() text.mark_set('here','1.2') text.insert('here','') text.insert('here','') mainloop()

结果: 

    

  只有mark_unset()方法可以解除,否则删除别的文本,Mark仍然在

 

from tkinter import * root = Tk() text = Text(root,width=20,height=5) text.insert(INSERT,'i love you') text.pack() text.mark_set('hear','1.2') text.insert('hear','') #text.mark_unset('hear')若要删除必须要用这个方法
text.delete('1.0',END) text.insert('hear','')

结果:

    

  Tags:通常用于改变Text组件中的内容的样式和功能

  tag_add():为指定的文本添加Tags

  tag_config():可以设置Tags的样式

 

from tkinter import * root = Tk() text = Text(root,width=50,height=5) text.pack() text.insert(INSERT,'原来你是我最想留住的幸运') text.tag_add('tag1','1.10','1.12') text.tag_config('tag1',background='yellow',foreground='red') mainloop()

 结果:

      

 

  Tags还支持事件绑定,用的是tag_bind()方法。

 

参看文献:

  小甲鱼的python视频教程

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM