在窗口里生成一个文本框
,文本框里的内容是可以编辑的:
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 开始