Python Forum
Tkinter - Issues with "iconbitmap" method - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Tkinter - Issues with "iconbitmap" method (/thread-26967.html)



Tkinter - Issues with "iconbitmap" method - aquerci - May-20-2020

hi guys,

below my code:
#!/usr/bin/python3

from tkinter import *
from tkinter import ttk

class MainWindow:
    
    def __init__(self, parent):    
        self.parent = parent
        parent.title("My Software")
        parent.iconbitmap("icon.ico")
        parent.geometry("530x515+360+200")
        parent.resizable (width=False, height=False)
        parent.configure(background="#f0f0f0")
        tools = IntVar()
        tools.set(1)
        ttk.Radiobutton(self.parent, text = "Menu 1 (my window has an icon!)", variable = tools, value = 1).pack()
        ttk.Radiobutton(self.parent, text = "Menu 2 (my window hasn't an icon!)", variable = tools, value = 2).pack()
        self.tools = tools
        ttk.Button(self.parent, text = "OK", command = self.__OpenTool).pack()
        ttk.Button(self.parent, text = "Settings", command = self.__OpenSettings).pack()
        
    def __OpenTool(self):
        tool = self.tools.get()
        if tool == 1:
            FirstWindow(self.parent)
            self.parent.withdraw()
        elif tool == 2:
            SecondWindow(self.parent)
            self.parent.withdraw()

    def __OpenSettings(self):
        SixthWindow(self.parent)


class FirstWindow:
    
    def __init__(self, parent):     
        self.parent = parent
        self.window = Toplevel(parent=None)
        self.window.title("My Software - Menu 1 (my window has an icon!)")
        self.window.iconbitmap("icon.ico")
        self.window.geometry("530x515+360+200")
        self.window.resizable (width=False, height=False)
        self.window.configure(background="#f0f0f0")
        self.window.protocol("WM_DELETE_WINDOW", self.__CloseEvent)
    
    def __CloseEvent(self):
        self.parent.deiconify()
        self.window.destroy()


class SecondWindow:
    
    def __init__(self, parent):        
        self.parent = parent
        self.window = Toplevel(parent=None)
        self.window.title("My Software - Menu 2 (my window hasn't an icon!)")
        self.window.geometry("530x515+360+200")
        self.window.resizable (width=False, height=False)
        self.window.configure(background="#f0f0f0")
        self.window.protocol("WM_DELETE_WINDOW", self.__CloseEvent)
    
    def __CloseEvent(self):
        self.parent.deiconify()
        self.window.destroy()


class SixthWindow:
    
    def __init__(self, parent):      
        self.parent = parent
        self.window = Toplevel(parent=None)
        self.window.title("My Software - Settings")
        self.window.iconbitmap("icon.ico")
        self.window.geometry("530x515+360+200")
        self.window.resizable (width=False, height=False)
        self.window.configure(background="#f0f0f0")       
        self.window.protocol("WM_DELETE_WINDOW", self.__CloseEvent)
    
    def __CloseEvent(self):
        self.parent.deiconify()
        self.window.destroy()


def main():
    root = Tk()
    app = MainWindow(root)
    root.mainloop()

if __name__ == "__main__":
    main()
if you choose to open one of the two avalable menus, a new window will appear and the main window hides itself. it's the same for the new window opened via the "Settings" button, but in this case the main window will not disappear. the setting and first menu windows have an icon, instead the second menu window uses the default icon from Tkinter.

the software works, but there is a really annoying behaviour. if you open the first menu window first of the the other ones, it will start always below another window already opened from your operating system (see my gif attached). it happens just one time only with the first menu window. I think it depends by the "self.window.iconbitmap("icon.ico")" and "self.parent.withdraw()" statements, because this issue doesn't happen with the second menu and "Settings" windows

how can I fix this issue?


RE: Tkinter - Issues with "iconbitmap" method - Larz60+ - May-20-2020

you can make root window hidden with parent.withdraw()
should be able to add it right after line 9
to bring back again, use parent.deiconify()


RE: Tkinter - Issues with "iconbitmap" method - deanhystad - May-21-2020

Calling parent.withdraw() before creating the first or second window places the new window at the top. The other order of create then withdraw probably should leave the newly created window on top, but since this window loses focus when you call the parent window, who gets focus is completely up to the window manager.

Very strange that a window decoration has this subtle effect.


RE: Tkinter - Issues with "iconbitmap" method - aquerci - May-21-2020

(May-20-2020, 08:40 PM)Larz60+ Wrote: you can make root window hidden with parent.withdraw()
should be able to add it right after line 9
to bring back again, use parent.deiconify()

yeah, but they are already configured after the line 9.


(May-21-2020, 07:33 AM)deanhystad Wrote: Calling parent.withdraw() before creating the first or second window places the new window at the top. The other order of create then withdraw probably should leave the newly created window on top, but since this window loses focus when you call the parent window, who gets focus is completely up to the window manager.

Very strange that a window decoration has this subtle effect.

you are right, if I call "parent.withdraw()" before to create the new window, I solve the issue but.. I know, probably we are talking about some milliseconds, but this solution makes me to see always the main window closes and the second one appears. before, the process seemed faster, now opening a new window seems slower and I don't like it.