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 ...
随机推荐
- HTTP漫谈
一.说明 1.1 当前背景说明 很多web的书包括web安全的书都会有一章介绍http协议,我就总恶意揣测作者是在凑字数,一般都直接跳过去. 相比TCP/IP这种各字段基于数值代号的协议,http这种 ...
- Linux下c语言TCP多线程聊天室
开发环境:Linux,GCC 相关知识:TCP(博客:传送门),线程 附加:项目可能还有写不足之处,有些bug没调出来(如:对在线人数的控制),希望大佬赐教. 那么话不多说,放码过来: 码云:传送门, ...
- 【Linux】CentOS7 打开关闭防火墙及端口
一.centos7版本对防火墙进行加强,不再使用原来的iptables,启用firewalld1.firewalld的基本使用启动: systemctl start firewalld查状态:syst ...
- Mysql——查看数据库,表占用磁盘大小
.查询所有数据库占用磁盘空间大小 select TABLE_SCHEMA, concat(,),' MB') as data_size, concat(,),'MB') as index_size f ...
- 显示 Uncaught TypeError: Cannot read property 'dialog' of undefined”的错误解决方法
最近在做一个基于easyUI的列表,新增功能的弹出框是以这样的方式: 运行测试的时候,报了这一堆的错误Uncaught TypeError: Cannot read property 'dialog' ...
- 在linux上安装elasticsearch简称ES 简单介绍安装步骤
1.简介 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 ...
- pycharm_python_flask相关学习心得逐步更新
2019-10-30: Pycharm的interpreter配置问题对于安装第三方库,如果能够在配置的可视化界面安装成功更好.如果不能可视化安装,则在pycharm的terri..仿cmd下用pip ...
- 三分钟掌握,使用Quqrtz.Net实现定时发送邮件
在实际的项目中,常遇到延时触发工作以及定时触发工作 这里所讲的是借助第三方的组件 Quartz.Net 来实现(源码位置:https://github.com/quartznet/quartznet) ...
- 【8】学习C++之this指针
在学习类的时候,我们可以考虑到一种情况: class Array { public: Array(int len); ~Array(); void setLen(int len) { len=len; ...
- Git 多人协作 以及推送分支
参考链接:https://www.liaoxuefeng.com/wiki/896043488029600/900375748016320 当你从远程仓库克隆时,实际上Git自动把本地的仓库的mast ...