Python Learner
Python Learner

Reputation: 487

iconify in tkinter in python

I have written a code in python 2.7 and i have created a root window with two buttons: submit and cancel when i press submit button, a new window opens and previous parent/root window gets iconify. Now i want that when i cut the child window, parent window should be deiconify, i am in a trouble that where to put the condition so that parent window gets deiconify?

from Tkinter import *

root = Tk()
root.title("test window")
root.geometry('400x220+500+250')
root.configure(background="dark gray")

def submit(*args):
    root.iconify()
    popup_root_window = Toplevel()
    popup_root_window.geometry('300x50+550+300')
    popup_root_window.resizable(width=False, height=False)
    popup_root_window.focus_force()
    popup_root_window.grab_set()
    popup_root_window_label = Label(popup_root_window, text="new window open successfully.")
    popup_root_window_label.pack(anchor=CENTER, padx=10, pady=20)

frame = Frame(root, bd=4, relief="raise", height=100, width=250)
frame.pack(fill="both", padx=70, pady=35)
frame.pack_propagate(0)

submit_button = Button(frame, text="Submit", command=submit, width=10)
submit_button.grid(row=0, column=0)

cancel_button = Button(frame, text="Cancel", width=10)
cancel_button.grid(row=0, column=1)

root.mainloop()

Upvotes: 1

Views: 2819

Answers (2)

Tech Learner
Tech Learner

Reputation: 241

You can try this one. i have tested this in python 2.7.13 and it working correctly.

from Tkinter import *

root = Tk()
root.title("test window")
root.geometry('400x220+500+250')
root.configure(background="dark gray")

def submit(*args):

    def on_closing(*args):
        popup_root_window.destroy()
        root.deiconify()

    root.iconify()
    popup_root_window = Toplevel()
    popup_root_window.geometry('300x50+550+300')
    popup_root_window.resizable(width=False, height=False)
    popup_root_window.focus_force()
    popup_root_window.grab_set()
    popup_root_window_label = Label(popup_root_window, text="new window open successfully.")
    popup_root_window_label.pack(anchor=CENTER, padx=10, pady=20)

    popup_root_window.protocol("WM_DELETE_WINDOW", on_closing)

frame = Frame(root, bd=4, relief="raise", height=100, width=250)
frame.pack(fill="both", padx=70, pady=35)
frame.pack_propagate(0)

submit_button = Button(frame, text="Submit", command=submit, width=10)
submit_button.grid(row=0, column=0)

cancel_button = Button(frame, text="Cancel", width=10)
cancel_button.grid(row=0, column=1)

root.mainloop()

Upvotes: 2

Ethan Field
Ethan Field

Reputation: 4740

I wrote the below in 3.6 but it should still work in 2.7.

from tkinter import *

class App:
    def __init__(self, root):
        self.root = root
        self.label1 = Label(self.root, text="I'm your main window.")
        self.button = Button(self.root, text="Submit", command=self.command)
        self.label1.pack()
        self.button.pack()
    def command(self):
        self.root.iconify()
        self.top = Toplevel(self.root)
        self.label2 = Label(self.top, text="I'm your toplevel window.")
        self.label2.pack()
        self.top.protocol("WM_DELETE_WINDOW", self.close)
    def close(self):
        self.top.destroy()
        self.root.deiconify()

root = Tk()
App(root)
root.mainloop()

You can use .protocol() on the event WM_DELETE_WINDOW to create a callback whenever the X is selected from the title bar of the designated window.

This is tested and working with Python 3.6 on Windows 10.

Upvotes: 2

Related Questions