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

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

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

 1 # encoding: utf-8
 2 import time
 3 from Tkinter import *
 4 import threading
 5 
 6 
 7 def write(file1,file2):
 8     with open(file1, 'r+') as f1:
 9         for line in f1:
10             # print line
11             f2 = open(file2, 'a+')
12             f2.write(line)
13             time.sleep(0.00001)
14 
15 
16 def windows1(file2):
17     root = Tk()
18     root.title("serial log")
19     s1 = Scrollbar(root)
20     s1.pack(side=RIGHT, fill=Y)
21     s2 = Scrollbar(root, orient=HORIZONTAL)
22     s2.pack(side=BOTTOM, fill=X)
23     textpad = Text(root, yscrollcommand=s1.set, xscrollcommand=s2.set, wrap='none')
24     textpad.pack(expand=YES, fill=BOTH)
25     s1.config(command=textpad.yview)
26     s2.config(command=textpad.xview)
27     with open(file2) as f:
28         while True:
29             line = f.readline()
30             textpad.pack()
31             textpad.insert(END, line)
32             textpad.see(END)
33             root.update()
34 if __name__ == '__main__':
35     file1 = 'log.txt'
36     file2 = 'result.txt'
37     threads = []
38     t1 = threading.Thread(target=write,args=(file1,file2))
39     threads.append(t1)
40     t2 = threading.Thread(target=windows1,args=(file2,))
41     threads.append(t2)
42     for t in threads:
43         # t.setDaemon(True)
44         t.start()
45     # for t in threads:
46     #     t.join()
View Code

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

正确代码:

调用两个类

 1 # encoding: utf-8
 2 import time
 3 from Tkinter import *
 4 import threading
 5 
 6 class MyThread(threading.Thread):
 7 
 8     def __init__(self, func, args):
 9         threading.Thread.__init__(self)
10         self.func = func
11         self.args = args
12 
13     def run(self):
14         self.func(*self.args)
15 
16 
17 def write(file1,file2):
18     with open(file1, 'r+') as f1:
19         for line in f1:
20             # print line
21             f2 = open(file2, 'a+')
22             f2.write(line)
23             time.sleep(0.00001)
24 
25 
26 def windows1(file2):
27     root = Tk()
28     root.title("serial log")
29     s1 = Scrollbar(root)
30     s1.pack(side=RIGHT, fill=Y)
31     s2 = Scrollbar(root, orient=HORIZONTAL)
32     s2.pack(side=BOTTOM, fill=X)
33     textpad = Text(root, yscrollcommand=s1.set, xscrollcommand=s2.set, wrap='none')
34     textpad.pack(expand=YES, fill=BOTH)
35     s1.config(command=textpad.yview)
36     s2.config(command=textpad.xview)
37     with open(file2) as f:
38         while True:
39             line = f.readline()
40             textpad.pack()
41             textpad.insert(END, line)
42             textpad.see(END)
43             root.update()
44 
45 
46 if __name__ == '__main__':
47     file1 = 'log.txt'
48     file2 = 'result.txt'
49     threads = []
50     t1 = MyThread(write, (file1,file2))
51     threads.append(t1)
52     t2 = MyThread(windows1,(file2,))
53     threads.append(t2)
54     for t in threads:
55         t.setDaemon(True)
56         t.start()
57     for t in threads:
58         t.join()
View Code

调用一个类一个class

 1 # encoding: utf-8
 2 import time
 3 from Tkinter import *
 4 import threading
 5 
 6 class MyThread(threading.Thread):
 7 
 8     def __init__(self, func, args):
 9         threading.Thread.__init__(self)
10         self.func = func
11         self.args = args
12 
13     def run(self):
14         self.func(*self.args)
15 
16 class write(object):
17 
18     def __int__(self,file1,file2):
19 
20         self.file1 = file1
21         self.file2 = file2
22         self.write_txt()
23 
24     def write_txt(self):
25         with open(self.file1, 'r+') as f1:
26             for line in f1:
27                 # print line
28                 f2 = open(self.file2, 'a+')
29                 f2.write(line)
30                 time.sleep(0.00001)
31 
32 
33 def windows1(file2):
34     root = Tk()
35     root.title("serial log")
36     s1 = Scrollbar(root)
37     s1.pack(side=RIGHT, fill=Y)
38     s2 = Scrollbar(root, orient=HORIZONTAL)
39     s2.pack(side=BOTTOM, fill=X)
40     textpad = Text(root, yscrollcommand=s1.set, xscrollcommand=s2.set, wrap='none')
41     textpad.pack(expand=YES, fill=BOTH)
42     s1.config(command=textpad.yview)
43     s2.config(command=textpad.xview)
44     with open(file2) as f:
45         while True:
46             line = f.readline()
47             textpad.pack()
48             textpad.insert(END, line)
49             textpad.see(END)
50             root.update()
51 
52 
53 if __name__ == '__main__':
54 
55     file1 = 'C:UsersyuxinglxDownloads\test\2018-07-16log.txt'
56     file2 = 'result.txt'
57     w = write()
58 
59     threads = []
60     t1 = MyThread(w.__int__, (file1,file2,))
61     threads.append(t1)
62     t2 = MyThread(windows1,(file2,))
63     threads.append(t2)
64     for t in threads:
65         t.setDaemon(True)
66         t.start()
67     for t in threads:
68         t.join()
View Code

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

原文地址:https://www.cnblogs.com/anita-harbour/p/9333946.html