目录
- 1. 按
- 2. 核心代码
- 3. 测试结果
- 4. 完整代码
1. 按
本文主要演示了python3 tkinter.scrolledtext.ScrolledText的对象在插入数据后如何将焦点设置在末尾,当然开发者用户如有需要也可以将焦点设置在其他地方。另外演示系统还展示了如何清空该对象的所有数据。
2. 核心代码
注意:此处的END在tkinter包中,直接使用需要先导入。
3. 测试结果
4. 完整代码
from
tkinter
import
Tk
from
tkinter
import
Button
from
tkinter
import
INSERT
from
tkinter
import
END
from
tkinter
import
WORD
from
tkinter
import
BOTH
from
tkinter
.
scrolledtext
import
ScrolledText
class
MainScene
(
)
:
def
__init__
(
self
)
:
self
.
win
=
Tk
(
)
self
.
initWin
(
)
self
.
log
=
ScrolledText
(
self
.
win
,
wrap
=
WORD
,
height
=
29
)
self
.
log
.
pack
(
side
=
"bottom"
,
fill
=
BOTH
)
self
.
initBtn
(
)
self
.
win
.
mainloop
(
)
def
initWin
(
self
)
:
self
.
win
.
title
(
"测试系统 by Coco56"
)
self
.
win
.
geometry
(
"600x600"
)
self
.
win
.
resizable
(
width
=
False
,
height
=
False
)
def
initBtn
(
self
)
:
Button
(
self
.
win
,
text
=
"插入一百条数据"
,
command
=
self
.
confimCB
)
.
place
(
x
=
150
,
y
=
180
)
Button
(
self
.
win
,
text
=
"清空日志"
,
command
=
self
.
emptyLog
)
.
place
(
x
=
280
,
y
=
180
)
def
confimCB
(
self
)
:
for
i
in
range
(
0
,
100
)
:
self
.
insertToLog
(
'Confirm '
+
str
(
i
)
)
;
def
insertToLog
(
self
,
str
)
:
self
.
log
.
insert
(
INSERT
,
str
+
'\n'
)
;
self
.
log
.
see
(
END
)
;
def
emptyLog
(
self
)
:
self
.
log
.
delete
(
0.0
,
END
)
;
self
.
insertToLog
(
'已清空日志'
)
if
__name__
==
'__main__'
:
MainScene
(
)