Python---tkinter之Text()

Text()介绍:点击打开链接



e.g.1 创建一个窗口组件,插入到text中

[python] view plain copy
print ?
  1. from tkinter import *  
  2.   
  3. root=Tk()  
  4.   
  5. text=Text(root,width=30,height=5)  
  6. text.pack()  
  7.   
  8. text.insert(INSERT,’I Love\n’)#INSERT表示在光标位置插入  
  9. text.insert(END,’FishC.com!\n’)#END表示在末尾处插入  
  10.   
  11.   
  12. def show():  
  13.     print(‘我被点了~’)  
  14.   
  15. #创建一个窗口组件,插入到text中  
  16. b1=Button(text,text=’点我点我’,command=show)  
  17. text.window_create(INSERT,window=b1)  
  18.   
  19.   
  20. mainloop()  
from tkinter import *

root=Tk()

text=Text(root,width=30,height=5)
text.pack()

text.insert(INSERT,'I Love\n')#INSERT表示在光标位置插入
text.insert(END,'FishC.com!\n')#END表示在末尾处插入


def show():
    print('我被点了~')





创建一个窗口组件,插入到text中

b1=Button(text,text='点我点我',command=show)
text.window_create(INSERT,window=b1)

mainloop()


e.g.2 事件绑定,链接到该网址

[python] view plain copy
print ?
  1. from tkinter import *  
  2. import webbrowser  
  3.   
  4. root=Tk()  
  5.   
  6. text=Text(root,width=30,height=5)  
  7. text.pack()  
  8.   
  9. text.insert(INSERT,’I love FishC.com!’)  
  10.   
  11. text.tag_add(’link’,‘1.7’,‘1.16’)  
  12. text.tag_config(’link’,foreground=‘blue’,underline=True)  
  13.   
  14. #事件绑定  
  15. def show_arrow_cursor(event):  
  16.     text.config(cursor=’arrow’)  
  17.   
  18. def show_xterm_cursor(event):  
  19.     text.config(cursor=’xterm’)  
  20.       
  21. def click(event):  
  22.     webbrowser.open(’http://www.fishc.com’)  
  23.   
  24. text.tag_bind(’link’,‘<Enter>’,show_arrow_cursor)  
  25. text.tag_bind(’link’,‘<Leave>’,show_xterm_cursor)  
  26. text.tag_bind(’link’,‘<Button-1>’,click)  
  27.   
  28. mainloop()  
from tkinter import *
import webbrowser

root=Tk()

text=Text(root,width=30,height=5)
text.pack()

text.insert(INSERT,'I love FishC.com!')

text.tag_add('link','1.7','1.16')
text.tag_config('link',foreground='blue',underline=True)





事件绑定

def show_arrow_cursor(event):
text.config(cursor=’arrow’)

def show_xterm_cursor(event):
text.config(cursor=’xterm’)

def click(event):
webbrowser.open(‘http://www.fishc.com‘)

text.tag_bind(‘link’,’<Enter>’,show_arrow_cursor)
text.tag_bind(‘link’,’<Leave>’,show_xterm_cursor)
text.tag_bind(‘link’,’<Button-1>’,click)

mainloop()

说明:当鼠标点击FishC.com时就会打开该链接


e.g.3检查输入的文本是否有修改

[python] view plain copy
print ?
  1. from tkinter import *  
  2. import hashlib  
  3.   
  4. root=Tk()  
  5.   
  6. text=Text(root,width=30,height=5)  
  7. text.pack()  
  8.   
  9. text.insert(INSERT,’I love FishC.com!’)  
  10. contents=text.get(’1.0’,END)  
  11.   
  12. def getSig(contents):  
  13.     m=hashlib.md5(contents.encode())  
  14.     return m.digest()  
  15.   
  16. sig=getSig(contents)  
  17.   
  18. def check():  
  19.     contents=text.get(’1.0’,END)  
  20.     if sig!=getSig(contents):  
  21.         print(‘警告:内容发生变化’)  
  22.     else:  
  23.         print(‘风平浪静~’)  
  24.   
  25. Button(root,text=’检查’,command=check).pack()  
  26.   
  27. mainloop()  
from tkinter import *
import hashlib

root=Tk()

text=Text(root,width=30,height=5)
text.pack()

text.insert(INSERT,'I love FishC.com!')
contents=text.get('1.0',END)

def getSig(contents):
    m=hashlib.md5(contents.encode())
    return m.digest()

sig=getSig(contents)

def check():
    contents=text.get('1.0',END)
    if sig!=getSig(contents):
        print('警告:内容发生变化')
    else:
        print('风平浪静~')

Button(root,text='检查',command=check).pack()

mainloop()

说明:把F删除了




  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值