-1

It looks like if you type:

from tkinter import *

messagebox.showinfo("App", "test")

There will be 2 windows open instead of one. Why? And How can I fix it?

GjdGr8
  • 133
  • 1
  • 1
  • 7
  • _"I get the problem..."_ - what is the problem? What is it doing that is different than you expect? – Bryan Oakley Jun 19 '20 at 04:21
  • In ```add_books``` function, a new window called Tk opens, that is the problem. You can run it your self, and press on add a book, and then you will see the problem. – GjdGr8 Jun 19 '20 at 05:18
  • Why do you destroy and recreate the main window? Simply hide it before showing another window, and then show it back when the another window is closed. – acw1668 Jun 19 '20 at 05:21
  • With hiding too, the tk window still opens even though i couldn't find its origin in my code. This only happens in the ```add_books()``` and ```check()``` functions, Any idea why? – GjdGr8 Jun 19 '20 at 06:38

2 Answers2

0

There is no way to destroy it knowing only the title, though I suppose you could iterate over all known widgets looking for one with a given title.

What you want to do instead is save a reference to the window, and then call destroy() on the reference. In the following example, root is a reference to the window created by Tk().

import tkinter as tk
root = tk.Tk()

Later, you can destroy that window with the following:

root.destroy()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • The problem is, root is my main widow. The new Tk window comes when I add a message.showinfo(). Any idea why that happens?? – GjdGr8 Jun 19 '20 at 01:35
  • And my question is something like this but the answer isn't correct as i used windows I guess. https://stackoverflow.com/questions/17280637/tkinter-messagebox-without-window – GjdGr8 Jun 19 '20 at 01:44
  • @GjdGr8: I don't understand your question. The root window is a window. The call to `showinfo()` opens a window. So, you should have two windows (assuming you create the root window first). Also, tkinter behaves pretty much exactly the same on all platforms. – Bryan Oakley Jun 19 '20 at 03:45
  • Okay...... The problem is I create a ```root = Tk()``` Then I do ```root.destroy()``` then I do ```message.showinfo()``` and it shows two windows instead of one. How do I fix this? And plus after the message is closed I create root again. – GjdGr8 Jun 19 '20 at 03:50
  • @GjdGr8: we can only go by what you posted in your question. If your question was inaccurate or left out details, there's no way we can give better answers. Every window must have a root. If you destroy the root, a new root will be created when you try to create another widget. – Bryan Oakley Jun 19 '20 at 03:51
  • The ```message.showinfo()``` doesn't need one I think. – GjdGr8 Jun 19 '20 at 03:52
0

The behavior you said is normal based on your updated code. If you don't want the root window being shown, create it manually and hide it before calling messagebox.showinfo(), then destroy the root window after the showinfo() is closed:

from tkinter import *
from tkinter import messagebox

root = Tk()
root.withdraw()
messagebox.showinfo("App", "test")
root.destroy()
acw1668
  • 40,144
  • 5
  • 22
  • 34