Python学习笔记0217(tkinter系列)

1、添加选项与删除
from tkinter import *

master=Tk()

theLB=Listbox(master)
theLB.pack()

for item in [“苹果”,“香蕉”,“草莓”,“菠萝”]:
theLB.insert(END,item)
#theLB.delete(0,END)#删除全部
#单独删除某一个
theButton=Button(master,text=“删除它”,
command=lambda x=theLB:x.delete(ACTIVE))
theButton.pack()
mainloop()
Python学习笔记0217(tkinter系列)
(1)拓展
Listbox(master,selectmode=SINGLE) #单选selecteode=SINGLE

theLB=Listbox(master,selectmode=EXTENDED) #多选selecteode=EXTENDED
Python学习笔记0217(tkinter系列)
from tkinter import *
master=Tk()

theLB=Listbox(master,selectmode=EXTENDED,height=11)
theLB.pack()

for item in range(11):
theLB.insert(END,item)

mainloop()
(2)滚动条
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()
Python学习笔记0217(tkinter系列)
(3)范围设置获取
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()
Python学习笔记0217(tkinter系列)
拓展,设置步长
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()
Python学习笔记0217(tkinter系列)
2、text主键学习笔记0217
(1)插入文本
from tkinter import *

root=Tk()

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

text.insert(INSERT,“我是谁”)
text.insert(END,“爱你爱你!”)

mainloop()
Python学习笔记0217(tkinter系列)
(2)插入主键
from tkinter import *

root=Tk()

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

text.insert(INSERT,“我是谁 \n”)
text.insert(END,“爱你爱你!”)

def show():
print(“我要飞得更高!”)
b1=Button(text,text=“点我吧”,command=show)
text.window_create(INSERT,window=b1)

mainloop()
Python学习笔记0217(tkinter系列)
(3)插入图片
from tkinter import *

root=Tk()

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

photo=PhotoImage(file=“7.gif”)
def show():
text.image_create(END,image=photo)

b1=Button(text,text=“点我吧”,command=show)
text.window_create(INSERT,window=b1)

mainloop()
Python学习笔记0217(tkinter系列)
(4)text主键的拓展(indexes、mark用法)
text.insert(INSERT,“wi are sister”)

text.mark_set(“here”,“1.2”)
text.mark_gravity(“here”,LEFT)
text.insert(“here”,“很”)
text.insert(“here”,“爱”)