Python Forum
Tkinter - Issues with "iconbitmap" method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter - Issues with "iconbitmap" method
#1
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?

Attached Files

Thumbnail(s)
   
Reply
#2
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()
Reply
#3
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.
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 437 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter
  Tkinter menu font size -method to change tonycat 2 1,193 Oct-11-2020, 02:43 AM
Last Post: tonycat
  tkinter get method is not accepting value when called by function jagasrik 1 396 Sep-16-2020, 05:28 AM
Last Post: Yoriz
  tkinter get method is not accepting value jagasrik 4 404 Sep-15-2020, 05:41 PM
Last Post: jagasrik
  Tkinter Listbox tab char issues ashtona 4 2,416 Mar-27-2018, 12:28 PM
Last Post: ashtona
  Widget placement issues with tkinter grid thread 1 mgtheboss 2 2,595 Jan-09-2018, 03:59 PM
Last Post: SmokerX
  Help with tkinter Button and Label interaction issues ... sealyons 0 3,198 Jun-01-2017, 06:58 PM
Last Post: sealyons

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020