一张gif演示一下scrollText插入数据的效果:
使用方式如下代码所示:
# coding=utf-8
import tkinter as tk
import threading
import time
import datetime
from tkinter import scrolledtext
def threadToUpdate():
while True:
time.sleep(0.2)
scr.insert(tk.END,"现在的时间是"+str( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') )+"\n")
return
def updateStatus():
t1 = threading.Thread(target=threadToUpdate)
t1.setDaemon(True)
t1.start()
return
root = tk.Tk() # 初始化Tk()
root.title("测试") # 设置窗口标题
root.geometry("700x600") # 设置窗口大小 注意:是x 不是*
updateBtn = tk.Button(root, text="更新状态",bd=2,width=10,font = 'Helvetica -16',command=updateStatus)
updateBtn.place(x=300, y=5, anchor='nw')
scr = scrolledtext.ScrolledText(root, width=50, height=20,font=("隶书",18)) #滚动文本框(宽,高(这里的高应该是以行数为单位),字体样式)
scr.place(x=50, y=50) #滚动文本框在页面的位置
root.mainloop() # 进入消息循环