How to close Toplevel window after the function it calls completes? - python

Edit:
let me include my code so I can get some specific help.
import Tkinter
def goPush():
win2=Tkinter.Toplevel()
win2.geometry('400x50')
Tkinter.Label(win2,text="If you have prepared as Help describes select Go otherwise select Go Back").pack()
Tkinter.Button(win2,text="Go",command=bounceProg).pack(side=Tkinter.RIGHT,padx=5)
Tkinter.Button(win2, text="Go Back", command=win2.destroy).pack(side=Tkinter.RIGHT)
def bounceProg():
d=1
print d
root=Tkinter.Tk()
root.geometry('500x100')
Tkinter.Button(text='Go', command=goPush).pack(side=Tkinter.RIGHT,ipadx=50)
root.mainloop()
So when you run the program it opens a window that says Go. Then Go opens a window that asks if youve read the help(which I didnt include in this code sample) and offers Go Back(which goes back) and Go. When you select Go it calls a function which prints 1. After it prints 1 I want the Window to close returning to the original window containing the Go button. How do I do such a thing?

#Kosig It won't quit root. Ie. self.foo = tk.Toplevel(self) and then self.foo.destroy()
For example:
class Foo(tk.Frame):
"""Foo example"""
def __init__(self, master=None):
"""Draw Foo GUI"""
tk.Frame.__init__(self, master)
self.grid()
self.draw_window_bar()
def draw_window_bar(self):
"""Draw bar TopLevel window"""
self.window_bar = tk.Toplevel(self)
# Some uber-pythonian code here...
ask_yes_or_no = messagebox.askyesno('Brian?', 'Romani Ite Domum')
if not ask_yes_or_no:
self.window_bar.destroy()
You have one main object, which is Foo. Foo has one main window (called "frame"), which it gets from tk.Frame. Afterwards, all Toplevel windows (frames) must be created within it. So, your new window here is self.window_bar and all its "objects" are in there, including the method for destroying it (self.window_bar.destroy()). You can call self.window_bar.destroy() from any part of the code, but here it is called after the user clicks "no".

If you create a toplevel window with the Toplevel command, you destroy it with the destroy method of the window object. For example:
import Tkinter as tk
class MyToplevel(tk.Toplevel):
def __init__(self, title="hello, world", command=None):
tk.Toplevel.__init__(self)
self.wm_title(title)
button = tk.Button(self, text="OK", command=lambda toplevel=self: command(toplevel))
button.pack()
if __name__ == "__main__":
def go(top):
print "my work here is done"
top.destroy()
app = tk.Tk()
t = MyToplevel(command=go)
t.wm_deiconify()
app.mainloop()

Apparently you just call quit on the root object that's running your mainloop
edit: All Tkinter widgets have a destroy() method which destroys that widget and its children. So you should be able to call this on your Toplevel

Related

How to pause the calling function that opens a child window until the child window is closed

I have a (hopefully) simple tkinter question, but in pouring over stack overflow, google, and other sites, all my searches seem to result in answers on multithreading which I don't think is the issue here.
Basically when I open a child window from a function in the main window, that calling function continues to execute, but I want it to pause when the child window is opened and resume when it's destroyed. Much like in "normal" (ie not tkinter) code, one function call executes and returns before the rest of the code is executed.
This is the essence of the code:
class ChildWindow(self, mainwin):
def __init__(self):
# build child window with tk.Toplevel(mainwin)
# Get input from entry box
# destroy()
class myGUI:
def __init__(self):
# Typical window setup stuff - canvas and buttons etc
def canvas_clicked(self):
# get data from canvas
ChildWindow()
print('This prints whilst the child window is still open')
print('But I want the Child Window to close before anything below the ChildWindow() call is executed')
print('Basically I want to print to screen what is put into the entry box in the Child Window')
Is this possible? My solution at the moment is to put those print statements in another routine which I call from from the child window right before ChildWindow.destroy() but this seems clunky - I'm sure there's a more elegant way without getting into complex multithreading stuff....
thanks
You can use wait_window method to pause the execution of the main window while the child window is open. Here is an example of how to modify your code:
class ChildWindow:
def init(self, master):
self.master = master
self.top = tk.Toplevel(master)
self.entry = tk.Entry(self.top)
self.entry.pack()
self.button = tk.Button(self.top, text='OK', command=self.close)
self.button.pack()
self.top.wait_window()
def close(self):
self.top.destroy()
class myGUI:
def init(self, master):
self.master = master
self.canvas = tk.Canvas(self.master)
self.canvas.pack()
self.canvas.bind('<Button-1>', self.canvas_clicked)
def canvas_clicked(self, event):
ChildWindow(self.master)
print('The text in the entry box is:', ChildWindow.entry.get())
root = tk.Tk()
app = myGUI(root)
root.mainloop()
The wait_window method will block the execution of the main window until the child window is destroyed. Once the child window is destroyed, the execution of the main window will resume and the print statement will display the text entered in the child window. Note that you need to pass the master argument (i.e., the root window) to the ChildWindow class to create the child window as a Toplevel widget.

Is there a way to reopen a window after closing it using destroy() in tkinter?

window6.after(1,lambda:window6.destroy())
is what I've been using to close my windows, is there any way to get them back after doing this?
basically, is there something that is the opposite of this?
ps. these are the libraries that I've imported, if it helps in any way
import tkinter as tk
from tkinter import *
import time
from tkinter import ttk
Is there a way to reopen a window after closing it using destroy() in tkinter?
The short answer is "no". Once it has been destroyed, it is impossible to get back. You should either create the window via a frame or class so that it's easy to recreate, or hide the window by calling .withdraw() rather than .destroy().
If you put the window code into a class or a function then after destroying it you can create a new instance of it by
1: creating a new instance of the class with the window code in the init function
2: call the function the has the code for the window
By doing this you are essentially creating a new instance of the program, but without initiating the script.
from tkinter impot *
from tkinter import ttk
#creating window function, not class
def main_window():
#window code here
root = Tk()
Label(root, text = "Hello World").pack()
#destroying main window
root.destroy()
root.mainloop()
main_window()
Of course, there are a few hurdles such as the window shutting down as soon as it opens, but this is to show that you can create a new instance of a window from your program.
You can wait for user input to see whether or not the window will open or close.
If you took an OOP approach, you can pass a reference to the Parent Widget as argument to the New_Window and store it in a class attribute.
You´ll have a two way reference: Parent knows child and child knows parent.
Then you can set the Parent Reference to the New_Window to None, from within the child Widget self.parent.new_window = None in a close_me() method right after you call self.destroy() on the New_Window:
1st Bonus: this code prevents the opening of more than 1 instance of a Window at a time. You won´t get more than 1 New_Window on the screen. I don´t think having two loggin windows opened or two equal options window makes sense.
2nd Bonus: It is possible to close the window from other parts of the code, as in a MVC patter, the Controller can close the window after doing some processing.
Here´s a working example:
import tkinter as tk
class Toolbar(tk.Frame):
'''Toolbar '''
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
# to store the New Window reference
self.new_window = None
self.button_new_window = tk.Button(self, text = 'New Window', command = lambda : self.get_window(self))
self.configure_grid()
def configure_grid(self):
'''Configures the Grid layout'''
self.grid(row=1, column=0, columnspan=3, sticky=(tk.N,tk.S,tk.E,tk.W))
self.button_new_window.grid(row = 2, column = 2, padx=5, pady=5)
def get_window(self, parent):
''' If window exists, return it, else, create it'''
self.new_window = self.new_window if self.new_window else Window(parent)
return self.new_window
class Window(tk.Toplevel):
'''Opens a new Window.
#param parent -- tk.Widget that opens/reference this window
'''
def __init__ (self, parent : tk.Widget):
# Stores reference to the Parent Widget, so you can set parent.new_window = None
self.parent = parent
super().__init__(master = parent.master)
self.title('New Window')
self.button_dummy = tk.Button(self, text = 'Do the thing', width = 25, command = lambda : print("Button pressed on window!"))
self.button_close = tk.Button(self, text = 'Close', width = 25, command = self.close_me)
self.configure_grid()
def configure_grid(self):
'''Grid'''
self.button_dummy.grid(row = 1, column = 0)
self.button_close.grid(row = 2, column = 0)
def close_me(self):
'''Tkinter widgets are made of two parts. 1. The python Object and 2. The GUI Widget.
The destroy() method gets rid of the widget part, but leaves the object in memory.
To also destroy the object, you need to set all of its references count to ZERO on
the Parent Widget that created the new Window, so the Garbage Collector can collect it.
'''
# Destroys the Widget
self.destroy()
# Decreasses the reference count on the Parent Widget so the Garbage Collector can destroy the python object
self.parent.new_window = None
if __name__ == '__main__':
root = tk.Tk()
toolbar = Toolbar(root)
root.mainloop()
I don´t know if this: .destroy() and re-instantiate approach is more efficient than the .withdraw() and .deiconify(). Maybe if you have a program that runs for long periods of time and opens a lot of windows it can be handy to avoid stackoverflow or heapoverflow.
It sure frees up the object reference from memory, but it has the additional cost of the re-instantiation, and that is processing time.
But as David J. Malan would say on CS50, “There´s always a tradeoff”.

Tkinter widgets lost information when I open a new instance of their window

In my real software I have a main menu (the main window) and others ones (toplevel windows) that the user can open using some widgets placed in the main one. it seems work, the main window can open the other toplevel windows, but I saw a really big issue. when I open more than one window for the same menu, all of them, except the last one, lost always the information placed in their widgets (in my case, Entry and ComboBox widgets). let's start with a simple example:
from tkinter import *
from tkinter import ttk
class MainWindow:
def __init__(self):
# load a "SecondWindow" object:
self.obj=SecondWindow(self)
# main window's gui:
self.parent=Tk()
self.parent.geometry("300x280+360+200")
self.parent.title("main window")
self.parent.configure(background="#f0f0f0")
self.OkButton=ttk.Button(self.parent, text="open the second window", width=26, command=lambda:self.obj.GUI())
self.OkButton.place(x=20, y=20)
self.parent.mainloop()
class SecondWindow:
def __init__(self, mw):
self.mw=mw
def GUI(self):
self.window=Toplevel(self.mw.parent)
self.window.geometry("300x180+360+200")
self.window.title("second window")
self.window.configure(background="#f0f0f0")
self.MenuSV=StringVar()
self.MenuSV.set("test test test")
self.MenuComboBox=ttk.Combobox(self.window, state="readonly", values=("ciao", "hola", "hello", "Salut"), textvariable=self.MenuSV)
self.MenuComboBox.place(x=20, y=20)
self.window.mainloop()
# start the program:
if __name__ == "__main__":
my_gui=MainWindow()
this code works like my real software. before to open the main window, a SecondWindow object is loaded (his main component is the GUI function). when you open the second window for just one time (using the SeconWindow object loaded before), it's ok, no issues, but if you open another one, the first one lost the information placed in his widgets. why?
I really don't understand this weird behaviour. how can I solve the issue?
Since you created only one instance of SecondWindow() inside MainWindow, so whenever GUI() of SecondWindow is executed, self.MenuSV will be reassigned another instance of StringVar(), so the previously created instance of StringVar() has no variable reference to it and it is garbage collected.
You can either create new instance of SecondWindow() whenever the button is clicked:
class MainWindow:
def __init__(self):
# load a "SecondWindow" object:
#self.obj=SecondWindow(self)
# main window's gui:
self.parent=Tk()
self.parent.geometry("300x280+360+200")
self.parent.title("main window")
self.parent.configure(background="#f0f0f0")
self.OkButton=ttk.Button(self.parent, text="open the second window", width=26,
command=lambda:SecondWindow(self).GUI()) # create new instance of SecondWindow here
self.OkButton.place(x=20, y=20)
self.parent.mainloop()
Or keep a reference of self.MenuSV inside SecondWindow:
class SecondWindow:
def __init__(self, mw):
self.mw=mw
def GUI(self):
self.window=Toplevel(self.mw.parent)
self.window.geometry("300x180+360+200")
self.window.title("second window")
self.window.configure(background="#f0f0f0")
self.MenuSV=StringVar()
self.MenuSV.set("test test test")
self.MenuComboBox=ttk.Combobox(self.window, state="readonly", values=("ciao", "hola", "hello", "Salut"), textvariable=self.MenuSV)
self.MenuComboBox.place(x=20, y=20)
self.MenuComboBox.MenuSV = self.MenuSV # keep a reference
self.window.mainloop()

tkinter run function in parent window when child window closes

I have a parent window in tkinter that opens a child window like this:
def openChild(data):
callChildFunc(data,"y")
Button(dash, text="Open", command=partial(openChild, data))
This calls a function callChildFunc(data,fill) which takes input and saves it to a json file. The child window closes like this:
def getInput(file):
#bunch of commands to save data from input fields...
childform.destroy()
Button(childform, text="Save", command=partial(getInput, data))
I want to call a function in the parent window when the child window is destroyed. Is there any way for the parent window to detect when the child is destroyed, or a way to pass an argument from the destroyed window back to the parent?
Found a solution that does what I want, execute a function when the child window is destroyed:
child.bind("<Destroy>",parentfunction())
You could just use a global variable, to which getInput could save it's data, and the parent window could then access afterwards.
If you don't mind rewriting your code, another way of doing it would be to use classes to manage your windows, and opening the child as a TopLevel widget ie.:
import tkinter as tk
class parent(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
button1 = tk.Button(self, text="Open", command=lambda : self.openChild())
# class variable for saving input of child
self.data = None
def openChild(self):
self.child = tk.TopLevel()
# entry box for data
self.entry1 = tk.Entry(self.child)
# button for saving data
button1 = tk.Button(self.child, text='Save', command= lambda : self.getInput())
def getInput(self):
if self.child:
self.data = self.entry1.get()
self.child.destroy()
Don't know if that's the most elegant way of handling that, but using classes gives you a lot more options for tkinter, it's worth a look.

TKInter checkbox variable is always 0

I'm using Python's TkInter module for a GUI. Below is a simple checkbox code.
def getCheckVal():
print cbVar.get()
windowTime=Tk.Tk()
cbVar = Tk.IntVar()
btnC = Tk.Checkbutton(windowTime, text="Save", variable = cbVar, command=getCheckVal)
btnC.grid()
windowTime.mainloop()
This code works fine. Each time I tick the checkbox, I get 1, else 0.
However, when I run the same code in a function that is called from another TkInter command (when a button is pressed), it stops working. I always get 0 as the value.
class GUIMainClass:
def __init__(self):
'''Create the main window'''
self.window = Tk.Tk()
def askUser(self):
def getCheckVal():
print cbVar.get()
windowTime=Tk.Tk()
cbVar = Tk.IntVar()
btnC = Tk.Checkbutton(windowTime, text="Save", variable = cbVar,
command=getCheckVal)
btnC.grid()
windowTime.mainloop()
def cmdWindow(self):
frameShow=Tk.Frame(self.window)
frameShow.grid()
btnSwitch = Tk.Button(frameShow, text='Show Plots', command=self.askUser)
btnSwitch.grid()
self.window.mainloop()
GUIObj=GUIMainClass()
GUIObj.cmdWindow()
This is very unusual. What could be going wrong?
EDIT: I've used 2 mainloops because I want a separate window (windowTime) to open up when I click "Show Plots" button. This new window should have the checkbox in it.
Your windowTime, cbVar, etc. variables are defined in the function's local scope. When askUser() completes execution, those values are thrown away. Prepend self. to them to save them as instance variables.
There should only be one mainloop() in your program, to run the main Tkinter root object. Try putting it as the very last line in the program. I recommend doing some reading on Effbot for how to set up a Tkinter application.
I'm not sure what all you're trying to do, but one problem is that the TK.IntVar called cbVar that you create in your askUser() method will be deleted when the function returns, so you need to attach it to something that will still exist after that happens. While you could make it a global variable, a better choice would be to make it an attribute of something more persistent and has a longer "lifespan".
Another likely issue is that generally there should only be one call to mainloop() in a single Tkinter application. It appears what you want to do is display what is commonly known as a Dialog Window, which Tkinter also supports. There's some standard ones built-in, plus some more generic classes to simplify creating custom ones. Here's some documentation I found which describes them in some detail. You may also find it helpful to look at their source code.
In Python 2 it's in the /Lib/lib-tk/tkSimpleDialog.py file and
in Python 3 the code's in a file named /Lib/tkinter/simpledialog.py.
Below is code that takes the latter approach and derives a custom dialog class named GUIButtonDialog from the generic one included the Tkinter library which is simply named Dialog.
try:
import Tkinter as Tk # Python 2
from tkSimpleDialog import Dialog
except ModuleNotFoundError:
import tkinter as Tk # Python 3
from tkinter.simpledialog import Dialog
class GUIButtonDialog(Dialog):
"""Custom one Button dialog box."""
def __init__(self, btnText, parent=None, title=None):
self.btnText = btnText
Dialog.__init__(self, parent, title)
def getCheckVal(self):
print(self.cbVar.get())
def body(self, master):
"""Create dialog body."""
self.cbVar = Tk.IntVar()
self.btnC = Tk.Checkbutton(master, text=self.btnText, variable=self.cbVar,
command=self.getCheckVal)
self.btnC.grid()
return self.btnC # Return the widget to get inital focus.
def buttonbox(self):
# Overridden to suppress default "OK" and "Cancel" buttons.
pass
class GUIMainClass:
def __init__(self):
"""Create the main window."""
self.window = Tk.Tk()
def askUser(self):
"""Display custom dialog window (until user closes it)."""
GUIButtonDialog("Save", parent=self.window)
def cmdWindow(self):
frameShow = Tk.Frame(self.window)
frameShow.grid()
btnSwitch = Tk.Button(frameShow, text='Show Plots', command=self.askUser)
btnSwitch.grid()
self.window.mainloop()
GUIObj = GUIMainClass()
GUIObj.cmdWindow()

Categories