【小甲鱼python】Tkinter学习笔记5-6

1.1
选中便删除
【小甲鱼python】Tkinter学习笔记5-6_第1张图片【小甲鱼python】Tkinter学习笔记5-6_第2张图片

from tkinter import*

master=Tk()

#theLB=Listbox(master,selectmode=EXTENDED)按住ctrl可以多选但还是只能删除一个。。
#theLB=Listbox(master,selectmode=SINGLE)单选
theLB=Listbox(master)
theLB.pack()

'''
theLB.insert(0,"西瓜")
theLB.insert(END,"荔枝")
'''

for item in ["鸡蛋","水果","牛肉","鹅肉"]:
    theLB.insert(END,item)#每一次都插到END的位置,END就是每一项的最后

'''
theLB.delete(0,END)#删除所有位置

theLB.delete(1)#删除第二个
'''

#选中哪一个就删除哪一个
theButton=Button(master,text="删除它",\
                 command=lambda x=theLB:x.delete(ACTIVE))#ACTIVE表示当前选中的值

theButton.pack()

mainloop()

1.2
当需要下拉才能浏览完所有选项时的解决方法

from tkinter import*

master=Tk()

theLB=Listbox(master,selectmode=EXTENDED,height=11) #height定义为有多少行
theLB.pack()


for item in range(11):
    theLB.insert(END,item)#每一次都插到END的位置,END就是每一项的最后

mainloop()



1.3
添加滚动条
为了在某个组件上安装垂直滚动条,你需要做两件事:
1.设置该组件的yscrollbarcommand选项为Scrollbar组件的set()方法
2.设置Scrollbar组件的command选项为该组件的yview()方法

from tkinter import*

root=Tk()

sb=Scrollbar(root)
sb.pack(side=RIGHT,fill=Y)# Y轴填充

lb=Listbox(root,yscrollcommand=sb.set)

for i in range(1000):
    lb.insert(END,i)

lb.pack(side=RIGHT,fill=BOTH)

sb.config(command=lb.yview)

mainloop()

1.4
【小甲鱼python】Tkinter学习笔记5-6_第3张图片

from tkinter import *

root=Tk()

Scale(root,from_=0,to=42).pack()
Scale(root,from_=0,to=200,orient=HORIZONTAL).pack()

mainloop()

1.5
获取刻度位置

from tkinter import *

root=Tk()

s1=Scale(root,from_=0,to=42)
s1.pack()
s2=Scale(root,from_=0,to=200,orient=HORIZONTAL)
s2.pack()

def show():
    print(s1.get(),s2.get())
Button(root,text="获取位置",command=show).pack()
mainloop()

1.6
【小甲鱼python】Tkinter学习笔记5-6_第4张图片

from tkinter import *

root=Tk()

s1=Scale(root,from_=0,to=42,tickinterval=5,resolution=5,length=200).pack()
s2=Scale(root,from_=0,to=200,tickinterval=10,orient=HORIZONTAL,length=600).pack()

mainloop()

2.1
【小甲鱼python】Tkinter学习笔记5-6_第5张图片

from tkinter import *
root=Tk()

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

text.insert(INSERT,"I love\n")
text.insert(END,"rain!")

def show():
    print("哟,我被点了一下~")

b1=Button(text,text="点我点我",command=show)#第一个text是变量名
text.window_create(INSERT,window=b1)
          
mainloop()

2.2
【小甲鱼python】Tkinter学习笔记5-6_第6张图片

from tkinter import *

root=Tk()

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

photo=PhotoImage(file="daka.png")


def show():
   text.image_create(END,image=photo)

b1=Button(text,text="点我点我",command=show)#第一个text是变量名
text.window_create(INSERT,window=b1)
          
mainloop()

你可能感兴趣的:(python)