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

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. 10. Scala数据结构(上)-集合操作

    10.1 数据结构特点 10.1.1 Scala集合基本介绍 uml => 统一建模语言 1) Scala同时支持不可变集合和可变集合,不可变集合可以安全的并发访问 两个主要的包 不可变集合:s ...

  2. 菜刀连接一句话木马出现:`Cannot call assert() with string argument dynamically`错误

    前言 逆天还是上学那会玩渗透的,后来工作后就再也没碰了,所以用的工具还是以前经典款,这不,发现出问题了 问题 如果是PHP5则没有问题,如果是PHP7,会出现:Cannot call assert() ...

  3. 给定一个长度为N的数组,找出出现次数大于n/2,n/3的数,要求时间复杂度O(n),空间复杂度O(1)

    先讨论出现次数大于n/2的数字,如果这样的数字存在,那么这个数出现的次数大于其他数出现的次数的总和. 在数组A中,我们定义两个数据集合a1,a2.a1为出现次数大于n/2的数的集合,a2为其余数组成的 ...

  4. Maven distributionManagement 分发构件至远程仓库

    https://blog.csdn.net/qq827245563/article/details/82661583 maven发布到本地仓库,和私服https://blog.csdn.net/u01 ...

  5. 使用php函数防止SQL注入方法

    什么是SQL注入? SQL注入是指在你系统防御之外,某人将一段Mysql语句注入到你的数据库.注入通常发生在系统要求用户输入数据的时候,比如用户名的输入,用户可能输入的不是一个用户名,而是一段SQL语 ...

  6. C#多线程的同步与通信

    C#中使用lock和Monitor控制多线程对资源的使用,最常见的生产者和消费者问题就是多线程同步和通信的经典例子.了解C#多线程的同步与通信. 一.关于lock和Monitor lock可以把一段代 ...

  7. c#十进制转换

    1.方法定义 /// <summary> /// 十进制转换 /// </summary> /// <param name="hexChar"> ...

  8. Java Mockito 笔记

    Mockito 1 Overview 2 Maven 项目初始化 3 示例 3.1 第一个示例 3.2 自动 Mock 3.3 Mock 返回值 3.4 Mock 参数 3.5 自动注入 Mock 对 ...

  9. Ubuntu 下安装zsh和oh-my-zsh

    注意:安装前先备份/etc/passwd 一开始装oh-my-zsh我是拒绝的,因为这东西安装容易,卸载难,真的很难. Mac安装参考:http://www.cnblogs.com/EasonJim/ ...

  10. 第一章 Maven 安装配置

    Maven基于(POM)项目对象模型,通过一小段描述信息来管理项目的构建.文档.和报告的项目管理软件,类似于php 的管理构建工具composer. 有关详细的Maven学习,可以参考学习https: ...