Python学习(tkinter系列)

本文介绍了Python的tkinter库中Listbox控件的使用,包括添加和删除选项、设置选择模式以及添加滚动条。同时,还探讨了Text组件的插入文本、插入按键及图片的方法,以及text主键的indexes和mark用法。

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()
在这里插入图片描述
(1)拓展
Listbox(master,selectmode=SINGLE) #单选selecteode=SINGLE

theLB=Listbox(master,selectmode=EXTENDED) #多选selecteode=EXTENDED
在这里插入图片描述
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()
在这里插入图片描述
(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()
在这里插入图片描述
拓展,设置步长
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()
在这里插入图片描述
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()
在这里插入图片描述
(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()
在这里插入图片描述
(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()
在这里插入图片描述
(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”,“爱”)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值