小甲鱼--------------Tkinter窗口组件:Text + Indexes

在窗口里生成一个文本框

,文本框里的内容是可以编辑的:

from tkinter import *

root = Tk()

text = Text(root,width=50,height=2)

text.insert(INSERT,"I Love Chengdu\n")
text.insert(END,"pandas")
text.pack()

mainloop()

在这里插入图片描述

在Text里增加一个按钮,代码该如何实现呢?

注意直接在创建的按钮后写b.pack(),是不会显示在text里的
在这里插入图片描述
可以理解为 在Text文本里新创建了一个窗口window,在window里存放了一个按钮

from tkinter import *

root = Tk()

text = Text(root, width=50, height=4)
text.pack()
text.insert(INSERT, "I Love Chengdu\n")
text.insert(END, "pandas")


def show():
    print("我被点击了一下")


b1 = Button(text, text="点我一下试试", command=show)

text.window_create(INSERT, window=b1)

mainloop()

在Text 里增加一个图片,怎么实现呢?

在这里插入图片描述

from tkinter import *

root = Tk()

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


photo = PhotoImage(file="19.gif")

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


b1 = Button(text, text="点我一下试试", command=show)

text.window_create(INSERT, window=b1)

mainloop()

Indexes:

indexes 是用来指向Text 组件中文本的位置,跟Python的序列索引一样,Text组件索引也是对应实际字符之间的位置

line.column:
行号是以 1 开始,列号则以 0 开始

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你在狗叫什么、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值