Seite 1 von 1

Wie mache ich ein PhotoImage weg?

Verfasst: Donnerstag 25. Mai 2017, 10:59
von Alfons Mittelmeyer
Das funktioniert nicht:
widget['image'] = None

Das PhotoImage in den Garbagekollector zu werfen, hatte bei tkinter widgets funktioniert:
widget.image = None

Aber bei ttk Widgets führt das bald zur Exception

Diese Lösung ist auch keine:
widget['image'] = PhotoImage(data='')

Da ist zwar dann ein PhotoImage nicht mehr sichtbar, der Text des Buttons aber auch nicht

Wie sage ich einem widget, dass es kein Image mehr haben soll?

Re: Wie mache ich ein PhotoImage weg?

Verfasst: Donnerstag 25. Mai 2017, 12:21
von wuf
Hi Alfons

Hier etwas zum ausprobieren:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from functools import partial

try:
    # Tkinter for Python 2.xx
    import Tkinter as tk
    import ttk
except ImportError:
    # Tkinter for Python 3.xx
    import tkinter as tk
    from tkinter import ttk
    
APP_TITLE = "Flash Image"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 200


class Application(tk.Frame):

    def __init__(self, master):
        self.master = master
        tk.Frame.__init__(self, master)
        
        self.image = tk.PhotoImage(file="test_image.gif")

        self.tk_label = tk.Label(self, text='Hi!', image=self.image)
        self.tk_label.pack()

        self.ttk_label = ttk.Label(self, text='Hi!', image=self.image)
        self.ttk_label.pack()
        
        self.flash_image()
        
    def flash_image(self, state=False):
        
        if state:
            state = False
            self.tk_label['image'] = self.image
            self.ttk_label['image'] = self.image
        else:
            state = True
            self.tk_label['image'] = ''
            self.ttk_label['image'] = ''

        self.after(500, self.flash_image, state)
    
    
def main():
    app_win = tk.Tk()
    app_win.title(APP_TITLE)
    app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    
    app = Application(app_win).pack(expand=True)
    
    app_win.mainloop()
 
 
if __name__ == '__main__':
    main()      
Gruss wuf :wink:

Re: Wie mache ich ein PhotoImage weg?

Verfasst: Donnerstag 25. Mai 2017, 12:54
von Alfons Mittelmeyer
@wuf: Danke, es ist also '' und nicht None

Man macht halt irgendwann Änderungen und wundert sich dann, wenn es auf einmal nicht mehr geht, hatte wohl '' durch None ersetzt.