【问题标题】:Why are there two different results if I debug or run it?如果我调试或运行它,为什么会有两个不同的结果?
【发布时间】:2020-02-08 07:00:16
【问题描述】:

我做了一个简单的函数来在 tkinter 中加载一张网络照片。 抓取照片时,GUI 会提示“正在加载...”。

完成后,照片将覆盖此提示。 为了避免 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()

现在如果我调试这段代码,它可以显示图像。 但是如果我运行这段代码,它只会放大窗口大小而不显示图像。

【问题讨论】:

标签: python tkinter python-imaging-library python-multithreading


【解决方案1】:

我终于找到了我的问题。就像this 说的,我保留参考。 xx.image = img1 并解决我的问题。但真正让我困惑的是为什么调试器不会使用垃圾回收。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 2014-02-03
    • 2021-04-02
    相关资源
    最近更新 更多