0

I'm writing a python program and i need 2 windows to show up. But when i run the code the 2nd window doesn't show my custom icon. Does anybody know why this is? The code is bellow

from tkinter import *

root = Tk()
root.geometry("1200x730")
root.title("NoteWork")
img = Image("photo", file="assets/icon.png")
root.call("wm","iconphoto", root._w, img)

# functions
def options():
    op_win = Tk()
    op_win.geometry("600x500")
    op_win.title("Options")
    img2 = Image("photo", file="assets/icon.png")
    root.call("wm","iconphoto", op_win._w, img2)

    op_win.mainloop()

btn1 = Label(root, text="Options", command=options)
btn.pack(pady=20)

root.mainloop()


Mechinsam
  • 11
  • 5
  • You can't use a multiple instance of `Tk`, you need `Toplevel`. – Funpy97 Jun 05 '21 at 14:23
  • 1
    Thank you Marino this solved my problem – Mechinsam Jun 05 '21 at 14:28
  • @Marino That is incorrect. OP's problem could have been solved if they added `master=op_win` to `img2 = Image(...)` – TheLizzard Jun 05 '21 at 14:36
  • @TheLizzard There aren't reasons to use multiple instances of `Tk` for this simple task. Each instance of `Tk` activate a new tlc interpreter, a **good pratice** is to have just one instance of `Tk` even if it is possible to have two or more. Look at the answer of @Bryan Oakley: https://stackoverflow.com/questions/36141010/is-each-instance-of-tk-class-running-independent-of-each-other – Funpy97 Jun 05 '21 at 15:09
  • @Marino I was saying that your wording is wrong because you said that you *can't* use multiple instances of `Tk`. Also I disagree with some parts of that answers. As long as you always pass the `master` parameter when creating any tkinter variables/widgets, you will never encounter any problems using multiple instances of `Tk`. – TheLizzard Jun 05 '21 at 15:38
  • @TheLizzard My *can't* had to be interpreted as **bad pratice** not in absolute way, anyway i don't understand why you should use multiple instances of `Tk` to show a new window if *tktiner* provides `Toplevel` exactly for that reason. – Funpy97 Jun 05 '21 at 15:44
  • @Marino Don't want to argue here (it will take too long), but in some cases having multiple instances of `Tk` instead of being forced to withdraw the main window can be easier to deal with. Also just a fun note: `.mainloop()` will only return only when all tkinter windows (all `Tk`/`Toplevels`) have been destroyed. – TheLizzard Jun 05 '21 at 15:48
  • @TheLizzard Yes, each `.mainloop()` will return only when the first `Tk` instance is destroyed. Anyway, I understand your point of view and it could be right in some applications but in the most of cases a *GUI* needs a single instance of `Tk` and then multiple instances of `Toplevel`. – Funpy97 Jun 05 '21 at 16:05
  • @Marino `.mainloop()` returns when **all** tkinter windows (`Tk`s and `Toplevel`s) are destroyed. Look at [this](https://stackoverflow.com/questions/67007447/new-root-mainloop-doesnt-make-main-window-unresponsive) for more info. For all cases you can use new `Tk` instances. For some cases using `Toplevel`s can use more computational resources. – TheLizzard Jun 05 '21 at 16:40

0 Answers0