为什么调试或 运行 会有两个不同的结果?
Why are there two different results if I debug or run it?
我做了一个简单的函数来在 tkinter 中加载网络照片。
抓取图片时,GUI会提示"loading..."。
完成后,照片将覆盖此提示。
为了避免爬行时GUI冻结,我使用threading
模块来做到这一点。
这是一个最小的、可重现的例子。
import requests
import threading
import tkinter
from PIL import ImageTk,Image
from io import BytesIO
class MultiProcessGetResultWithoutArgs(threading.Thread): # get thread result
def __init__(self, func):
threading.Thread.__init__(self)
self.func = func
self.result = None
def getResult(self):
return self.result
def run(self):
self.result = self.func()
def GetOne():
return Image.open(BytesIO(requests.get('https://s2.ax1x.com/2020/02/07/12usP0.th.jpg').content))
def checkWhetherGet(): # judge whether it has result.
result = thread.getResult()
if result:
img1 = ImageTk.PhotoImage(result)
tkinter.Label(w,image=img1).grid(row=0,column=0)
w.update()
w.after_cancel(1)
else:
w.after(100,checkWhetherGet)
def about():
global w,thread
w = tkinter.Toplevel()
tkinter.Label(w,text="loading....").grid(row=0,column=0)
thread = MultiProcessGetResultWithoutArgs(GetOne)
thread.start() # non-block thread
w.after(1000,checkWhetherGet)
w.mainloop()
Win = tkinter.Tk()
tkinter.Button(Win,text="start",command=about).grid()
Win.mainloop()
现在如果我调试这段代码,它可以显示图像。
但是如果我 运行 这段代码,它只会放大 window 大小而不会显示图像。
终于找到我的problem.Likethis说了,留着参考。
xx.image = img1
并解决我的 problem.But 真正让我困惑的是为什么调试器不使用垃圾收集。
我做了一个简单的函数来在 tkinter 中加载网络照片。 抓取图片时,GUI会提示"loading..."。
完成后,照片将覆盖此提示。
为了避免爬行时GUI冻结,我使用threading
模块来做到这一点。
这是一个最小的、可重现的例子。
import requests
import threading
import tkinter
from PIL import ImageTk,Image
from io import BytesIO
class MultiProcessGetResultWithoutArgs(threading.Thread): # get thread result
def __init__(self, func):
threading.Thread.__init__(self)
self.func = func
self.result = None
def getResult(self):
return self.result
def run(self):
self.result = self.func()
def GetOne():
return Image.open(BytesIO(requests.get('https://s2.ax1x.com/2020/02/07/12usP0.th.jpg').content))
def checkWhetherGet(): # judge whether it has result.
result = thread.getResult()
if result:
img1 = ImageTk.PhotoImage(result)
tkinter.Label(w,image=img1).grid(row=0,column=0)
w.update()
w.after_cancel(1)
else:
w.after(100,checkWhetherGet)
def about():
global w,thread
w = tkinter.Toplevel()
tkinter.Label(w,text="loading....").grid(row=0,column=0)
thread = MultiProcessGetResultWithoutArgs(GetOne)
thread.start() # non-block thread
w.after(1000,checkWhetherGet)
w.mainloop()
Win = tkinter.Tk()
tkinter.Button(Win,text="start",command=about).grid()
Win.mainloop()
现在如果我调试这段代码,它可以显示图像。 但是如果我 运行 这段代码,它只会放大 window 大小而不会显示图像。
终于找到我的problem.Likethis说了,留着参考。
xx.image = img1
并解决我的 problem.But 真正让我困惑的是为什么调试器不使用垃圾收集。