tkinter中text文本与scroll滚动条控件(五)
text与scroll控件
1 import tkinter 2 3 wuya = tkinter.Tk() 4 wuya.title("wuya") 5 wuya.geometry("300x200+10+20") 6 7 # 创建文本框text,设置宽度100,high不是高度,是文本显示的行数设置为3行 8 text = tkinter.Text(wuya, width='30', height='3') 9 text.pack() 10 11 # 设置文本框内容 12 txt = 'China urges the U.S. to abide by the one-China principle and the principles of the three Sino-U.S.' \ 13 ' Joint Communiques, and stop all forms of military contact with Taiwan including arms sales, Wu said.' 14 # 将文本内容插入文本框 15 text.insert('insert',txt) 16 17 18 19 wuya.mainloop()
结果为:
可以观察到内容不太多,显示不下,加个滚动条使显示,可以上下滚动:
1 import tkinter 2 3 wuya = tkinter.Tk() 4 wuya.title("wuya") 5 wuya.geometry("300x50+10+20") 6 7 # 创建滚动条 8 scroll = tkinter.Scrollbar() 9 # 创建文本框text,设置宽度100,high不是高度,是文本显示的行数设置为3行 10 text = tkinter.Text(wuya) 11 # 将滚动条填充 12 scroll.pack(side=tkinter.RIGHT,fill=tkinter.Y) # side是滚动条放置的位置,上下左右。fill是将滚动条沿着y轴填充 13 text.pack(side=tkinter.LEFT,fill=tkinter.Y) # 将文本框填充进wuya窗口的左侧, 14 # 将滚动条与文本框关联 15 scroll.config(command=text.yview) # 将文本框关联到滚动条上,滚动条滑动,文本框跟随滑动 16 text.config(yscrollcommand=scroll.set) # 将滚动条关联到文本框 17 18 # 设置文本框内容 19 txt = 'China urges the U.S. to abide by the one-China principle and the principles of the three Sino-U.S.' \ 20 ' Joint Communiques, and stop all forms of military contact with Taiwan including arms sales, Wu said.' 21 # 将文本内容插入文本框 22 text.insert('insert',txt) 23 24 25 26 wuya.mainloop()
结果如下:
分类:
Tkinter
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具