Acamori
Acamori

Reputation: 347

Python tkinter can't use "pyimage4" as iconphoto

In my program I have a main window and I would like to add a loading window before that main window will initialize but I can't add icon for this loading window. There my code:

from tkinter import *
import tkinter.ttk as ttk

root = Tk()
root.withdraw()
root.title('BINGO')

class Loader():
    def __init__(self):
        self.loader = Tk()
        try:
            img = PhotoImage(r'C:\menu-16.png')
            self.loader.tk.call('wm', 'iconphoto', self.loader._w, img)
        except:
            self.loader.attributes('-toolwindow', True)
        self.loader.title('BINGO')
        self.frame = LabelFrame(self.loader)
        self.frame.pack(fill=BOTH, pady=2,padx=2)
        self.label = Label(self.frame, text='Loading...', font=('segoe', 12))
        self.label.pack(side=TOP, pady='4')
        self.progress_bar = ttk.Progressbar(self.frame, mode='determinate', length=464)
        self.progress_bar.pack(side=TOP, fill='x', pady='20')
        self.loader.update()

    def bar(self, value):
        self.progress_bar['value'] += value
        self.loader.update()

    def destroy(self):
        self.loader.destroy()

loader = Loader()
loader.bar(5)

class Main_Window():
    def __init__(self):
        self.mframe = LabelFrame(root)
        pass

loader.bar(100)

root.tk.call('wm', 'iconphoto', root._w, PhotoImage(r'C:\menu-16.png'))

loader.destroy()

Application = Main_Window()
root.mainloop()

In this case "loader" run as tool window without icon but main "Main_window" have it. How to fix this ?

Upvotes: 1

Views: 1351

Answers (2)

Acamori
Acamori

Reputation: 347

I'm found solution of my problem:

I had to change self.loader = Tk() into self.loader = Toplevel()

Upvotes: 1

Santosh Aryal
Santosh Aryal

Reputation: 1376

Check for your code. Progress bar is defined in tkinter.tkk so you need to import tkinter.tkk as

from tkinter import *
from tkinter.ttk import *
....
img = PhotoImage(r'C:\menu-16.png'))
...
root.tk.call('wm', 'iconphoto', root._w, PhotoImage(r'C:\menu-16.png'))

as there is an extra ), remove the ) and add file= to the PhotoImage and remove the ttk from ttk.Progressbar.

img = PhotoImage(file=r'C:\menu-16.png')
....
root.tk.call('wm', 'iconphoto', root._w, PhotoImage(file=r'C:\menu-16.png'))

And you have defined class as Main_Window() and you are calling as Main_window().

tkinter gui with progress bar

Upvotes: 0

Related Questions