【发布时间】:2019-12-31 12:18:04
【问题描述】:
我有一个 tkinter 应用程序和一个将一些数据写入文件的线程。如果我让线程完成它的工作,则文件为空。如果我在线程完成之前终止程序(单击 pyCharm 中的红色方块),则文件将充满数据直到终止点。这是重现问题的代码:
import tkinter as tk
import _thread
import numpy as np
img_list = []
def create_img_list():
for i in range(1000):
img = np.random.rand(385, 480)
img = img * 65535
img = np.uint16(img)
img_list.append(img)
def write_to_file():
f = open("test.Raw", "wb")
for img in img_list:
f.write(img)
f.close()
root = tk.Tk()
button = tk.Button(root, text="Click Me", command=_thread.start_new_thread(write_to_file, ())).pack()
create_img_list()
root.mainloop()
这是怎么回事,我该如何解决?
【问题讨论】:
-
@martineau 哪两个线程?
-
不要直接使用
_thread模块。你从不想直接访问下划线_modues,除非你确切地知道你在做什么。改用threading:command=threading.Thread(None, write_to_file, ()).start. -
@Dan 首先我初始化列表,然后在 button_click 上开始写入 不,你不是。
write_to_file首先被调用。您可以通过在每个函数中添加print(name)来检查它。 -
@Dan,你对线程做了什么尝试?我在评论中展示了什么?因为这对我有用。
-
将
print(img_list)添加到write_to_file(),您将看到此函数在开始时执行-无需单击按钮-甚至在运行create_img_list()之前创建列表,因此write_to_file()写入空列表。您在command=中创建并启动thead,并将结果从线程分配给command=,但您应该分配正常函数,该函数稍后将创建线程并启动它。或为此使用lambda-command=lambda:_thread.start_new_thread(write_to_file, ())
标签: python multithreading file tkinter