1

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 ?

Acamori
  • 327
  • 1
  • 5
  • 15
  • 1
    Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – stovfl Dec 27 '19 at 13:06

2 Answers2

1

I'm found solution of my problem:

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

Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
Acamori
  • 327
  • 1
  • 5
  • 15
0

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

Santosh Aryal
  • 1,276
  • 1
  • 18
  • 41
  • ty, this mistakes i had only here in example. In real code all clear and i'm still can't understand why loader can't initialize icon :( – Acamori Dec 27 '19 at 11:09
  • Comment the loader.destroy() to see it action. Its working with the icon. – Santosh Aryal Dec 27 '19 at 11:34
  • Here code, that is not my main but all works same. Try run it. https://drive.google.com/open?id=1tGE-Xx_3qGtYifoEjyNq2gIzCvR4kQwX – Acamori Dec 27 '19 at 11:48
  • The code works fine, just change the `self.loader.attributes('-toolwindow', True)` to `self.loader.attributes('-alpha', True)` since `-toolwindow` is not a valid attribute you can use these `-alpha`, `-fullscreen`, `-modified`, `-notify`, `-titlepath`, `-topmost`, or `-transparent`. – Santosh Aryal Dec 27 '19 at 11:59
  • this line take effect `self.loader.attributes('-toolwindow', True)` only if error in `self.loader.tk.call('wm', 'iconphoto', self.loader._w, img)`. Why tkinter cannot setup icon ? – Acamori Dec 27 '19 at 12:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204941/discussion-between-santosh-aryal-and-acamori). – Santosh Aryal Dec 27 '19 at 12:11