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

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. 小心!做 UI 自动化一定要跨过这些坑

    一 .引子 UI自动化,在移动互联网时代的今天,一直都是在各大测试社区最为火爆的一个TOPIC.甚至在测试同行面前一提起自动化,大家就会自然而然的问:“恩,你们是用的什么框架?appium?还是rob ...

  2. UML类图知识

  3. PyInstaller把Python脚本打包成可执行程序教程

    一.说明 一直以来都有把.py文件打包成.exe文件的想法,但总是不够强烈,每次拖着拖着就淡忘了. 昨天帮硬件部门的同事写了个脚本,然后今天下午的时候,他问有没有办法把脚本打包成可执行文件,这样方便以 ...

  4. [转帖]SQL Server 2000~2017补丁包

    SQL Server 2000~2017补丁包 https://www.cnblogs.com/VicLiu/p/11510510.html 最新更新 Product Version Latest S ...

  5. 不一样的LCA——luoguP1852跳跳棋

    洛谷端题目链接 loj端题目链接 题目大意: 在一条数轴上进行跳跳棋游戏.棋子只能摆在整点上.每个点不能摆超过一个棋子.用跳跳棋完成:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动 ...

  6. Scala 数组操作之Array、ArrayBuffer以及遍历数组

    ArrayBuffer 在Scala中,如果需要类似于Java中的ArrayList这种长度可变的集合类,则可以使用ArrayBuffer. // 如果不想每次都使用全限定名,则可以预先导入Array ...

  7. SPA单页面应用和MPA多页面应用(转)

    原文:https://www.jianshu.com/p/a02eb15d2d70 单页面应用 第一次进入页面时会请求一个html文件,刷新清除一下,切换到其他组件,此时路径也相应变化,但是并没有新的 ...

  8. Java中Integer和ThreadLocal

    一. Integer 1.引子 在开始之前,我还是需要吐槽下自己,我是真的很菜! 他问:**两个Integer对象==比较是否相等? 我答:对象==比较,是引用比较,不相等! 他问:IntegerCa ...

  9. Java3-5年经验面试题总结

    记录一下本次找工作所遇到的一些高频面试题,第一次找java工作,感觉比面试.net舒服多了,17年的时候出去找.net工作,由于在公司做的东西用到的技术少,除了mvc和ef,其他没啥问的,就追着项目问 ...

  10. 【转载】C#中SqlConnection类的作用以及常用方法

    在C#的数据库编程中,SqlConnection类主要用于连接Sqlserver数据库,使用SqlConnection类的实例方法我们可以打开Sqlserver数据库连接以及获取数据完毕后关闭数据库连 ...