python Tkinter的Text组件中创建x轴和y轴滚动条,并且text文本框自动更新(三)
要求对文件边读边写并显示对话框。
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文本框自动更新(三)的更多相关文章
- python Tkinter的Text组件中创建x轴和y轴滚动条
#!/usr/bin/python #coding: utf-8 from Tkinter import * root = Tk() root.title("记事本") root. ...
- Python实现双X轴双Y轴绘图
诈尸人口回归.这一年忙着灌水忙到头都掉了,最近在女朋友的提醒下终于想起来博客的账号密码,正好今天灌水的时候需要画一个双X轴双Y轴的图,研究了两小时终于用Py实现了.找资料的过程中没有发现有系统的文章, ...
- 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.文件读取.可变参数.自动滚动 之前的介 ...
- Winform中实现ZedGraph新增自定义Y轴上下限、颜色、标题功能
场景 Winform中实现ZedGraph的多条Y轴(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1001322 ...
- WInform中实现设置ZedGraph中曲线的X轴与Y轴的上限与下限
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- Winform中设置ZedGraph鼠标悬浮显示举例最近曲线上的点的坐标值和X轴与Y轴的标题
场景 Winform中设置ZedGraph鼠标双击获取距离最近曲线上的点的坐标值: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/ ...
- 统制Highcharts中x轴和y轴坐标值的密度
统制Highcharts中x轴和y轴坐标值的密度 www.MyException.Cn 发布于:2012-06-26 10:04:13 浏览:688次 1 控制Highcharts中x轴和y轴坐标值的 ...
- Winform中设置ZedGraph的X轴与Y轴的刻度不在对面显示
场景 C#窗体应用中使用ZedGraph曲线插件绘制图表: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99716066 Win ...
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(0):专题导航 [持续更新中]
写在前面的话 随着 Visual Studio 2013 的正式推出,ASP.NET 和 Visual Studio Web 开发工具 也发布了各自的最新版本. 新版本在构建 One ASP.NET ...
随机推荐
- Centos修改swap分区大小
1. 查看当前分区情况 free -m 2. 增加swap大小 dd if=/dev/zero of=/var/swap bs=1024 count=12288000 #增加12G空间 3. 设置交换 ...
- [记录]UserControl 的一个值得注意的问题 [属性" * "的代码生成失败.错误是:"程序集"*.Version=1.0.0.0,Culture=neutral,..........无标记为序列化"
开发时在做UserControl,需要注意的List<>集合问题~~~! 其他类型的集合可能也存在这样的问题,但是我没去测试,在写集合的时候一般List<>用的多点,所以经常碰 ...
- @AspectJ注解的value属性
@Component @Scope("prototype") @Aspect(value="perthis(execution(* com.helius.service. ...
- AntDesign vue学习笔记(三)嵌套路由使用
本项目目前结构如下 1.Login页面=>MainFrm页面=>MainFrm左部菜单,右边是显示区域可以切换子页面. 2.当点击左部菜单时,右边的子页面随着进行切换. 实现关键代码如下1 ...
- Shuffle an Array (水塘抽样)
随机性问题 水塘抽样算法可保证每个样本被抽到的概率相等 使用场景:从包含n个项目的集合S中选取k个样本,其中n为一很大或未知的数量,尤其适用于不能把所有n个项目都存放到主内存的情况 Knuth洗牌算法 ...
- Scala Types 1
在 Scala 中所有值都有一种对应的类型 单例类型 形式:value.type,返回类型 value / null 场景1:链式API调用时的类型指定 class Super { def m1(t: ...
- 移动开发首页业界资讯移动应用平台技术专题 输入您要搜索的内容 基于Java Socket的自定义协议,实现Android与服务器的长连接(二)
在阅读本文前需要对socket以及自定义协议有一个基本的了解,可以先查看上一篇文章<基于Java Socket的自定义协议,实现Android与服务器的长连接(一)>学习相关的基础知识点. ...
- Spring Boot 整合 MyBatis 实现乐观锁和悲观锁
本文以转账操作为例,实现并测试乐观锁和悲观锁. 完整代码:https://github.com/imcloudfloating/Lock_Demo GitHub Page:http://blog.cl ...
- Syste.IO命名空间下的流操作类之间的关系
- Options of the DB storage of prometheus
参考: // Options of the DB storage. type Options struct { // The timestamp range of head blocks after ...