78.刪除菜單中項目
from tkinter import *
root=Tk()
theLB=Listbox(root)
theLB.pack()
#theLB.insert(0,'pig')
#theLB.insert(END,'apple')
for item in ['haha','ooo','ususu','sjsjs']:
???theLB.insert(END,item)
theButton=Button(root,text='shanchu',command=lambda x=theLB:x.delete(ACTIVE))
theButton.pack()
mainloop()
解析:
定義一個listbox對象并利用其insert方法放入列表
定義一個按鈕,并定義當(dāng)按鈕觸發(fā)要進(jìn)行藍(lán)不大函數(shù)進(jìn)行闡述選中的菜單中項目Active就是告訴刪的是選中的那個項目
79.滾動條
from tkinter import *
root=Tk()
sb=Scrollbar(root)
sb.pack(side=RIGHT,fill=Y)
lb=Listbox(root,yscrollcommand=sb.set)
for i in range(1000):
???lb.insert(END,i)
lb.pack(side=LEFT,fill=BOTH)
sb.config(command=lb.yview)
mainloop()
80.scale組件
from tkinter import *
root=Tk()
Scale(root,from_=0,to=42,tickinterval=5,resolution=5,length=200).pack()
Scale(root,from_=0,to=200,tickinterval=10,orient=HORIZONTAL,length=600).pack()
mainloop()
解析:
tickinterval是刻度的間距
resolition是每次跳動的距離
orient是方向
81.text組件
單行文本用label組件鹊漠,多行選項用listbox組件捧韵,輸入框用entry烈钞,按鈕用button組件(radiobutton是單選組件付燥,checkbutton是多選組件职辅,frame和labelframe可以將多個組件組合成一個框架,滾動條組件是scrollbar和刻度組件scale(提供一個范圍,用戶可以選擇一個值)
1)text組件:顯示和處理多行文本
from tkinter import *
root=Tk()
text=Text(root,width=30,height=300)
text.pack()
text.insert(INSERT,'hahaha')#INSERT輸入光標(biāo)的位置
text.insert(END,'idnh')
photo=PhotoImage(file='timg.gif')
def show():
???print('inbwund')
???text.image_create(END,image=photo)
b1=Button(text,text='dianwo',command=show)#插入一個按鈕基于text
text.window_create(INSERT,window=b1)#利用text的window_create對象插入一個按鈕
mainloop()
2)tags用法:給某些字符加上標(biāo)簽司蔬,并對標(biāo)簽屬性設(shè)置
from tkinter import *
root=Tk()
text=Text(root,width=30,height=5)
text.pack()
text.insert(INSERT,'I love FishC.com')
text.tag_add('tag1','1.8','1.12','1.15')
text.tag_config('tag1',background='yellow',foreground='red')
mainloop()
3)tag與鼠標(biāo)事件進(jìn)行綁定
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.baidu.com')
text.tag_bind('link','',show_arrow_cursor)
text.tag_bind('link','',show_xterm_cursor)
text.tag_bind('link','',click)
mainloop()
4)自動檢測是否發(fā)生改變
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('biandong')
???else:
???????print('meishi')
Button(root,text='check',command=check).pack()
mainloop()
5)搜索文本內(nèi)容
from tkinter import *
import hashlib
root=Tk()
text=Text(root,width=30,height=5)
text.pack()
text.insert(INSERT,'I love FishC.com')
def getIndex(text,index):
???return tuple(map(int,str.split(text.index(index),'.')))
start='1.0'
while True:
???pos=text.search('o',start,stopindex=END)
???if not pos:
???????break
???else:
???????print('get weizhi :',getIndex(text,pos))
???????start=pos+'+1c'
mainloop()