Tkinter的Text控件如何给文本换行?

有python代码:

window = tk.Tk()
t = tk.Text(window2, height=15, width=65)
button = tk.Button(window,text='获 取 IP', command=getIP,width=15, height=2)

def getIP():
	# randomIP()的作用是从数据库获取一个ip地址 
    IP = t.insert('end',randomIP())

运行以后是这样的:
在这里插入图片描述

很显然,ip地址粘在一起了,如果我们要每读取一次ip就换行一次,那只需要加入语句:t.insert(tk.INSERT, '\n')即可

如下所示:

window = tk.Tk()
t = tk.Text(window2, height=15, width=65)
button = tk.Button(window,text='获 取 IP', command=getIP,width=15, height=2)

def getIP():
	# randomIP()的作用是从数据库获取一个ip地址 
    IP = t.insert('end',randomIP())
    t.insert(tk.INSERT, '\n')

修改后的效果:
在这里插入图片描述

发布了208 篇原创文章 · 获赞 841 · 访问量 121万+

猜你喜欢

转载自blog.csdn.net/baishuiniyaonulia/article/details/101553910