美文网首页
Python-GUI基础-03

Python-GUI基础-03

作者: 杨大菲 | 来源:发表于2018-01-21 18:02 被阅读0次

    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方法放入列表

    定义一个按钮,并定义当按钮触发要进行蓝不大函数进行阐述选中的菜单中项目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输入光标的位置

    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用法:给某些字符加上标签,并对标签属性设置

    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与鼠标事件进行绑定

    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)自动检测是否发生改变

    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)搜索文本内容

    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()

    相关文章

      网友评论

          本文标题:Python-GUI基础-03

          本文链接:https://www.haomeiwen.com/subject/wurqaxtx.html