Tkinter 中 Toplevel 里 Label 参数里面设置的图片对象 不能正常显示

    xiaoxiao2021-03-25  123

    最近遇到这样一个问题,在 Toplevel 中发现,我的图片不能正常的显示在窗口中.当时是这个样子

    recently i get a problem from toplevel function. my picture can’t display successful. like this (英文自己翻译的,意图通过这样的方法慢慢锻炼自己糟糕透了的英语,欢迎指出错误.)


    这个是当时出错的时候的代码

    this is my fault code at the time


    from tkinter import * #一定要注意的事情就是 grid 与 pack 不要混合使用!会带来麻烦的! root = Tk() def create_new_demo(): myimg = PhotoImage(file='bg.gif',width=100,height=100) newWindow = Toplevel() Label(newWindow,text='姓名:').grid(row=0,column=0) Label(newWindow,text='年龄:').grid(row=1,column=0) Label(newWindow,text='性别:').grid(row=2,column=0) Label(newWindow,image=myimg,bg='gray')\ .grid(row=0,column=1,rowspan=3) #利用 colspan 跨三行 Text(newWindow,width=30,height=6)\ .grid(row=3,column=0,columnspan=2) #利用 column 跨两列 Button(root,text='查看跨行跨列grid布局',command=create_new_demo).grid(row=2,column=1) mainloop()

    我的处理过程:

    发现问题:

    discovery problem


    当发现这个问题的时候,我也是挺纳闷的,所以我首先是在搜索引擎上搜索,貌似并没有人遇到像我这样的问题. 然后我开始在底层 root对象上尝试.能够成功显示.这将问题缩小到这个可能是因为 create_new_demo() 函数上.(when i discovery this problem,i’m very confused. searching in Search Engines but No-one have the same trouble) (then, i try to make picture display in root-object successfully ,it means the problem maybe caused by function create_new_demo() )

    尝试解决问题:

    try to resolve the problem


    我试着把里面的 myimg = PhotoImage(file=’bg.gif’,width=100,height=100) 语句移动到 create_new_demo() 之外.发现成功显示. (i try to move myimg = PhotoImage(file='bg.gif',width=100,height=100) to the outside of create_new_domo(),it display successfully) from tkinter import * #一定要注意的事情就是 grid 与 pack 不要混合使用!会带来麻烦的! root = Tk() myimg = PhotoImage(file='bg.gif',width=100,height=100) def create_new_demo(): # 在这里定义的图片是不会显示的,因为它会当做一个局部变量,当离开作用域的时候就会被清除掉 newWindow = Toplevel() Label(newWindow,text='姓名:').grid(row=0,column=0) Label(newWindow,text='年龄:').grid(row=1,column=0) Label(newWindow,text='性别:').grid(row=2,column=0) Label(newWindow,image=myimg,bg='gray')\ .grid(row=0,column=1,rowspan=3) #利用 colspan 跨三行 Text(newWindow,width=30,height=6)\ .grid(row=3,column=0,columnspan=2) #利用 column 跨两列 Button(root,text='查看跨行跨列grid布局',command=create_new_demo).grid(row=2,column=1) mainloop()

    推测:

    Speculation


    这个可能是由于垃圾回收机制造成的问题.当运行离开 create_new_demo() 作用域后,里面的 myimg 对象 被认为之后再也用不到.所以被垃圾回收机制给处理了.(maybe the problem caused by Garbage Collection,it recognize it will not use any more in the future, hence GC it)

    原理(我猜测性写的):

    #大家可以在 python3 中运行下观察现象. import time class Echo: def __init__(self): print('我被创建') def __del__(self): print('我被销毁') def create_new_demo(): target = Echo() if __name__ == '__main__': create_new_demo() while True: time.sleep(1)

    通过现象我们可以发现,当 create_new_demo() 被调用后的确有创建 target 对象,也正如我们的 图片 myimg 对象被创建.但是当离开作用域的时候,myimg 对象被当做垃圾回收了,所以我们看不见图片. (Through phenomenon we can find that target object created when call function create_new_demo() , just as our picture object created have the same situation .we can’t see the picture because of it resolve by GC when leave scope)

    转载请注明原文地址: https://ju.6miu.com/read-6721.html

    最新回复(0)