Mayank Jain
Mayank Jain

Reputation: 67

Python: secondary window is not destroying completely

There's a Tkinter project I'm doing for understanding both python and tkinter. I'm stuck at a situation where I call secondary popup window from main window and after the popup is being destroyed its not returning to main screen.

File mainScr.py

import tkinter as tk
import popup

root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry('%dx%d+%d+%d' % (width*0.8, height*0.8, width*0.1, height*0.1))

def popup():
    showPopup()
    print("popup destroyed")

show_btn = tk.Button(root, command= popup) 
show_btn.pack()

root.mainloop()

File popup.py

import tkinter as tk

class showPopup():
    def __init__(self):
        self.popup = tk.Toplevel()
        self.popup.title("Details")
        w = 400     # popup window width
        h = 250     # popup window height
        sw = self.popup.winfo_screenwidth()
        sh = self.popup.winfo_screenheight()
        x = (sw - w)/2
        y = (sh - h)/2
        self.popup.geometry('%dx%d+%d+%d' % (w, h, x, y))
        self.show()
        self.popup.mainloop()

    def save(self):        
        self.popup.destroy()

    def show(self):
        save_btn = tk.Button(self.popup, text="Save", command= self.save)
        save_btn.pack()

Above is the code of mine, I've called showPopup() class from main screen which creates new popup window using Toplevel() in tkinter. But even if I destroyed the popup window, it should return to main window and print "popup destroyed" but it isn't.

The popup is closing but print statement is not executed. And when I close main window, console then execute the print statement

Upvotes: 1

Views: 142

Answers (1)

James Huang
James Huang

Reputation: 876

After some messing around, I have found the solution.

(This is after you have made previous changes regarding my earlier comments which contained code that I later changed to fix this).

It appears that the main error comes from you declaring an object of class showPopup() in the popup() function.

The first fix I had to make was that showPopup was from another file. To fix this, I wrote popup.showPopup() However, this is not right because the code thinks it's a function.

To fix the problem above, I had to import popup in another way. since you are only using the showPopup class, simply do from popup import showPopup. Now get rid of the popup.showPopup() if you put that down already because that doesn't work.

Now, you just have to call save() on the class. To do this, I assigned your showPopup() class to a variable called new and then called new.save() under it.

I also removed popup.mainloop() because it isn't needed for TopLevel()'s

Full Code:

mainScr.py:

import tkinter as tk
from popup import showPopup

root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.geometry('%dx%d+%d+%d' % (width * 0.8, height * 0.8, width * 0.1, height * 0.1))


def popup():
    new = showPopup()
    new.save()
    print("popup destroyed")


show_btn = tk.Button(root, command=popup)
show_btn.pack()

root.mainloop()

popup.py:

import tkinter as tk


class showPopup():
    def __init__(self):
        self.popup = tk.Toplevel()
        self.popup.title("Details")
        w = 400  # popup window width
        h = 250  # popup window height
        sw = self.popup.winfo_screenwidth()
        sh = self.popup.winfo_screenheight()
        x = (sw - w) / 2
        y = (sh - h) / 2
        self.popup.geometry('%dx%d+%d+%d' % (w, h, x, y))

    def save(self):
        self.popup.destroy()

    def show(self):
        save_btn = tk.Button(self.popup, text="Save", command=self.save)
        save_btn.pack()

Upvotes: 2

Related Questions