程序员的知识教程库

网站首页 > 教程分享 正文

实例08:定位多行文本框内容位置(多行文本框必须设置哪两个属性)

henian88 2024-08-16 17:26:55 教程分享 8 ℃ 0 评论

1、 添加1个多行文本框,添加5个命令按钮

2、 点击不同命令按钮将红色的“Hello”插入在文本的不同位置

import tkinter as tk                 #导入tkinter
window=tk.Tk()                       #建立窗口window
window.title('插入Hello')
window.geometry('500x300+400+200')   #设定窗口大小(长宽左上)这里的乘是小x

t1=tk.Text(window,font=('Arial',14))     #建立多行文本框t1
t1.place(x=10,y=10,width=300,height=280) #设置多行文本框t1位置
t1.insert(1.0,'abcdefg\n')               #插入内容
t1.insert(2.0,'1234567\n')
t1.tag_config('c',foreground = 'red')    
def hit1():                #建立自定义函数hit1
    t1.insert(1.0,'Hello','c')
def hit2():                #建立自定义函数hit2
    t1.insert(tk.END,'Hello','c')
def hit3():                #建立自定义函数hit3
    t1.insert(2.4,'Hello','c')
def hit4():                #建立自定义函数hit4
    t1.insert(tk.INSERT,'Hello','c')    
def hit5():                #建立自定义函数hit5
    a=''
    t1.mark_set(a,tk.INSERT+'+2c')
    t1.insert(a,'Hello','c')  
bu1=tk.Button(window,text='插入在开头',font=('Arial',10),width=10,height=1,command=hit1)
bu1.place(x=380,y=10,width=100,height=40)   #设置命令按键位置
bu2=tk.Button(window,text='插入在结尾',font=('Arial',10),width=10,height=1,command=hit2)
bu2.place(x=380,y=60,width=100,height=40)  
bu3=tk.Button(window,text='插入在2行5列',font=('Arial',10),width=10,height=1,command=hit3)
bu3.place(x=380,y=110,width=100,height=40)   #设置命令按键位置
bu4=tk.Button(window,text='插入在光标位',font=('Arial',10),width=10,height=1,command=hit4)
bu4.place(x=380,y=160,width=100,height=40) 
bu5=tk.Button(window,text='插在光标后2位',font=('Arial',10),width=10,height=1,command=hit5)
bu5.place(x=380,y=210,width=100,height=40) 

window.mainloop()

字数达到300才算原创,所以加上几句废话,略过,略过。字数达到300才算原创,所以加上几句废话,略过,略过。字数达到300才算原创,所以加上几句废话,略过,略过。字数达到300才算原创,所以加上几句废话,略过,略过。字数达到300才算原创,所以加上几句废话,略过,略过。字数达到300才算原创,所以加上几句废话,略过,略过。字数达到300才算原创,所以加上几句废话,略过,略过。字数达到300才算原创,所以加上几句废话,略过,略过。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表