当前位置:网站首页>tkiner组件之滚动文本框(scrolledtext )相关操作
tkiner组件之滚动文本框(scrolledtext )相关操作
2022-08-09 13:07:00 【论一个测试的养成】
1.创建一个滚动文本框
import tkinter as tk
from tkinter import scrolledtext
root = tk.Tk()
root.title('text')
root.geometry('100x100')
scr = scrolledtext.ScrolledText(root,width=10,height=5)
scr.pack()
root.mainloop()
如图
2.读取text文件内容显示到滚动文本框
import tkinter as tk
from tkinter import scrolledtext
root = tk.Tk()
root.title('text')
root.geometry('300x100')
scr = scrolledtext.ScrolledText(root,width=50,height=5)
scr.pack()
with open('test') as f:
for line in f:
scr.insert('end',line)#末尾插入
#scr.insert('insert',line)#光标插入
root.mainloop()
如图:
3.文本内容的删除
import tkinter as tk
from tkinter import scrolledtext
root = tk.Tk()
root.title('text')
root.geometry('300x100')
scr = scrolledtext.ScrolledText(root,width=50,height=5)
scr.pack()
for i in range(10):
scr.insert('end', i)
scr.insert('end', '\n')
scr.delete(1.0) #删除第一个元素
scr.delete(1.0,'end') #删除所有元素
root.mainloop()
4.文本内容的获取
import tkinter as tk
from tkinter import scrolledtext
root = tk.Tk()
root.title('text')
root.geometry('300x100')
scr = scrolledtext.ScrolledText(root,width=50,height=5)
scr.pack()
for i in range(10):
scr.insert('end', i)
scr.insert('end', '\n') #
print(scr.get(1.0,'end')) #获取文本中所有内容
print(scr.get(1.0)) #获取文本中第一行的内容
root.mainloop()
5.打印选中的文本内容
import tkinter as tk
from tkinter import scrolledtext
root = tk.Tk()
root.title('text')
root.geometry('300x100')
scr = scrolledtext.ScrolledText(root,width=50,height=5)
scr.pack()
for i in range(10):
scr.insert('end', i)
scr.insert('end', '\n') #
def showselection():
try:
s = scr.selection_get()
except Exception as e:
print('....')
else:
print(s)
tk.Button(root,text='sava',command=showselection).pack()
root.mainloop()
如图:
边栏推荐
猜你喜欢

pytest 之 重运行机制与测试报告

FFmpeg多媒体文件处理(ffmpeg操作目录及list的实现)

FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)

蓝桥历届真题-既约分数

为什么文字不贴合边

WSA toolkit installed app store tip doesn't work how to solve?

FFmpeg multimedia file processing (the basic concept of ffmpeg processing stream data)

微服务+微信小程序实现社区服务

pytest 之 allure报告

GIN file upload and return
随机推荐
pytest 之 allure报告
R language kaggle game data exploration and visualization
Professor Chen Qiang's "Machine Learning and R Application" course Chapter 13 Assignment
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面(循环不变量)
[极客大挑战 2019]Upload
Q_08 更多信息
Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
An Offer 21. Adjust the array in order to make odd in even the front (loop invariant)
Q_04_05 使用Qubits
LeetCode 37.解数独
Come and throw eggs.
问题系列-如何修改或更新localhost里的值
X264性能优化
蓝桥历届真题-跑步锻炼
缓存和数据库一致性问题
FFmpeg多媒体文件处理(FFMPEG日志系统)
Q_04_06 把它放在一起:传送
乐东消防救援大队应邀为干部开展消防安全培训
ArcEngine(十)创建矢量图层
RobotFramework 之 Setup和Teardown