当前位置:网站首页>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()
如图:
边栏推荐
- 关于做2D游戏时,Canvas边界显示在Game窗口的问题
- An Offer 21. Adjust the array in order to make odd in even the front (loop invariant)
- Time series analysis course lab report
- 昇腾AI开发者创享日南京站!一起CANN机器狗+AI机械臂实现硬核智慧救援!燃爆现场~
- Q_06_05 文件结构
- CPU-MIPS32 instruction architecture (unlocked pipeline microprocessor)
- FPGA - Summary of bugs in ISE (in update)
- LeetCode 37. Solve Sudoku
- Unicom network management protocol block diagram
- NFS pays special attention to the problem of permissions
猜你喜欢
GIN a preliminary study, the environment is installed
GIN Bind mode to get parameters and form validation
Explanation of RTSP protocol
WSA toolkit installed app store tip doesn't work how to solve?
搭建大型分布式服务(二)搭建会员服务
RobotFramework简介
蓝桥杯线上模拟赛——Flex 经典骰子布局
快来扔鸡蛋。
Final assignment of R language data analysis in a university
pytest 筛选用例
随机推荐
海康设备获取YV12图像-不用rtsp
记录本项目中用到的系统调用与C库函数-2
NC7 买卖股票的最好时机(一)
offset、client、scroll、window.pageYOffset比较
2.微服务'黑话'集锦及Eureka注册中心相关概念
NC53 删除链表的倒数第n个节点
从房产中介到程序员--80后张江男
蓝桥历届真题-既约分数
剑指offer,剪绳子2
Q_06_01 Q#编程语言
IDEA Gradle 常遇问题(二)(持续更新)
为什么文字不贴合边
Q_04_07 进一步探索
Ledong Fire Rescue Brigade was invited to carry out fire safety training for cadres
ArcEngine(十)创建矢量图层
Process/Thread Related in Sandbox - 2
pytest 之 fixture的调用
X264性能优化
微服务+微信小程序实现社区服务
Record the system calls and C library functions used in this project-2