要求对文件边读边写并显示对话框。

1.加线程之后,必须要文件写完才显示对话框。错误代码:

 # encoding: utf-8
import time
from Tkinter import *
import threading def write(file1,file2):
with open(file1, 'r+') as f1:
for line in f1:
# print line
f2 = open(file2, 'a+')
f2.write(line)
time.sleep(0.00001) def windows1(file2):
root = Tk()
root.title("serial log")
s1 = Scrollbar(root)
s1.pack(side=RIGHT, fill=Y)
s2 = Scrollbar(root, orient=HORIZONTAL)
s2.pack(side=BOTTOM, fill=X)
textpad = Text(root, yscrollcommand=s1.set, xscrollcommand=s2.set, wrap='none')
textpad.pack(expand=YES, fill=BOTH)
s1.config(command=textpad.yview)
s2.config(command=textpad.xview)
with open(file2) as f:
while True:
line = f.readline()
textpad.pack()
textpad.insert(END, line)
textpad.see(END)
root.update()
if __name__ == '__main__':
file1 = 'log.txt'
file2 = 'result.txt'
threads = []
t1 = threading.Thread(target=write,args=(file1,file2))
threads.append(t1)
t2 = threading.Thread(target=windows1,args=(file2,))
threads.append(t2)
for t in threads:
# t.setDaemon(True)
t.start()
# for t in threads:
# t.join()

注:# t.setDaemon(True)禁掉是因为写文件只运行了一行。# t.join() 因为程序会卡死。错误原因start()使用需要重新写run()。

正确代码:

调用两个类

 # encoding: utf-8
import time
from Tkinter import *
import threading class MyThread(threading.Thread): def __init__(self, func, args):
threading.Thread.__init__(self)
self.func = func
self.args = args def run(self):
self.func(*self.args) def write(file1,file2):
with open(file1, 'r+') as f1:
for line in f1:
# print line
f2 = open(file2, 'a+')
f2.write(line)
time.sleep(0.00001) def windows1(file2):
root = Tk()
root.title("serial log")
s1 = Scrollbar(root)
s1.pack(side=RIGHT, fill=Y)
s2 = Scrollbar(root, orient=HORIZONTAL)
s2.pack(side=BOTTOM, fill=X)
textpad = Text(root, yscrollcommand=s1.set, xscrollcommand=s2.set, wrap='none')
textpad.pack(expand=YES, fill=BOTH)
s1.config(command=textpad.yview)
s2.config(command=textpad.xview)
with open(file2) as f:
while True:
line = f.readline()
textpad.pack()
textpad.insert(END, line)
textpad.see(END)
root.update() if __name__ == '__main__':
file1 = 'log.txt'
file2 = 'result.txt'
threads = []
t1 = MyThread(write, (file1,file2))
threads.append(t1)
t2 = MyThread(windows1,(file2,))
threads.append(t2)
for t in threads:
t.setDaemon(True)
t.start()
for t in threads:
t.join()

调用一个类一个class

 # encoding: utf-8
import time
from Tkinter import *
import threading class MyThread(threading.Thread): def __init__(self, func, args):
threading.Thread.__init__(self)
self.func = func
self.args = args def run(self):
self.func(*self.args) class write(object): def __int__(self,file1,file2): self.file1 = file1
self.file2 = file2
self.write_txt() def write_txt(self):
with open(self.file1, 'r+') as f1:
for line in f1:
# print line
f2 = open(self.file2, 'a+')
f2.write(line)
time.sleep(0.00001) def windows1(file2):
root = Tk()
root.title("serial log")
s1 = Scrollbar(root)
s1.pack(side=RIGHT, fill=Y)
s2 = Scrollbar(root, orient=HORIZONTAL)
s2.pack(side=BOTTOM, fill=X)
textpad = Text(root, yscrollcommand=s1.set, xscrollcommand=s2.set, wrap='none')
textpad.pack(expand=YES, fill=BOTH)
s1.config(command=textpad.yview)
s2.config(command=textpad.xview)
with open(file2) as f:
while True:
line = f.readline()
textpad.pack()
textpad.insert(END, line)
textpad.see(END)
root.update() if __name__ == '__main__': file1 = 'C:\Users\yuxinglx\Downloads\\test\\2018-07-16\log.txt'
file2 = 'result.txt'
w = write() threads = []
t1 = MyThread(w.__int__, (file1,file2,))
threads.append(t1)
t2 = MyThread(windows1,(file2,))
threads.append(t2)
for t in threads:
t.setDaemon(True)
t.start()
for t in threads:
t.join()

以上代码在python2.7.9上运行可行。

python Tkinter的Text组件中创建x轴和y轴滚动条,并且text文本框自动更新(三)的更多相关文章

  1. python Tkinter的Text组件中创建x轴和y轴滚动条

    #!/usr/bin/python #coding: utf-8 from Tkinter import * root = Tk() root.title("记事本") root. ...

  2. Python实现双X轴双Y轴绘图

    诈尸人口回归.这一年忙着灌水忙到头都掉了,最近在女朋友的提醒下终于想起来博客的账号密码,正好今天灌水的时候需要画一个双X轴双Y轴的图,研究了两小时终于用Py实现了.找资料的过程中没有发现有系统的文章, ...

  3. Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

    创建基于对话框的Windows应用程序(四)—— Edit Control.Combo Box的应用.Unicode转ANSI.Open File Dialog.文件读取.可变参数.自动滚动 之前的介 ...

  4. Winform中实现ZedGraph新增自定义Y轴上下限、颜色、标题功能

    场景 Winform中实现ZedGraph的多条Y轴(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1001322 ...

  5. WInform中实现设置ZedGraph中曲线的X轴与Y轴的上限与下限

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  6. Winform中设置ZedGraph鼠标悬浮显示举例最近曲线上的点的坐标值和X轴与Y轴的标题

    场景 Winform中设置ZedGraph鼠标双击获取距离最近曲线上的点的坐标值: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/ ...

  7. 统制Highcharts中x轴和y轴坐标值的密度

    统制Highcharts中x轴和y轴坐标值的密度 www.MyException.Cn 发布于:2012-06-26 10:04:13 浏览:688次 1 控制Highcharts中x轴和y轴坐标值的 ...

  8. Winform中设置ZedGraph的X轴与Y轴的刻度不在对面显示

    场景 C#窗体应用中使用ZedGraph曲线插件绘制图表: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99716066 Win ...

  9. 在 Visual Studio 2013 中创建 ASP.NET Web 项目(0):专题导航 [持续更新中]

    写在前面的话 随着 Visual Studio 2013 的正式推出,ASP.NET 和 Visual Studio Web 开发工具 也发布了各自的最新版本. 新版本在构建 One ASP.NET ...

随机推荐

  1. Centos修改swap分区大小

    1. 查看当前分区情况 free -m 2. 增加swap大小 dd if=/dev/zero of=/var/swap bs=1024 count=12288000 #增加12G空间 3. 设置交换 ...

  2. TreeMap源码分析2

    package map; import org.junit.Test; import com.mysql.cj.api.x.Collection; import map.TreeMap1.Ascend ...

  3. web开发-心路历程

    从事web开发已经有几年了,感触颇多,在此记录一下. 对于学习: 几年的经历让我认识到了,学习确实是一个持续永恒的过程.目前的社会发展很快,各种新的思想,新的机会不断刷新我的认知,也让我体会到了自己能 ...

  4. 微信小程序之使用函数防抖与函数节流

    函数防抖和函数节流都是老生常谈的问题了.这两种方式都能优化 js 的性能.有些人可能会搞混两个的概念.所以,我以自己的理解,来解释这两个概念的含义.并且列举在小程序中这两个方法的使用. 函数防抖: 英 ...

  5. 【模板】LCT

    核心思想: 动态维护一个森林.支持删边,加边,查询链信息等很多操作. 由若干棵$Splay$组成,每棵$Splay$维护一条链,以深度作为关键字. 也就是说$Splay$的中序遍历相当于从上到下遍历这 ...

  6. Linux中历史命令

    历史命令,即之前登录session会话中的在命令行键入的命令. 在Linux中可以有两种方式查询历史命令 history命令 当前用户目录中的隐藏文件.bash_history 一.history 作 ...

  7. golang ---查看进程(Windows)

    package main import ( "fmt" "os" "os/exec" "strconv" "s ...

  8. Java的jdk环境变量配置

    方法/步骤 1.安装JDK 选择安装目录 安装过程中会出现两次 安装提示 .第一次是安装 jdk ,第二次是安装 jre .建议两个都安装在同一个java文件夹中的不同文件夹中.(不能都安装在java ...

  9. Windwos利用批处理文件自动清理归档日志,计划任务

    首先在X盘根目录建立一个文档  cmd.txt 打开cmd.txt这个文本文件,在里面第一行写入 DELETE ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-3'; ...

  10. C#读写设置修改调整UVC摄像头画面-对比度

    有时,我们需要在C#代码中对摄像头的对比度进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄 ...