How to close a window if tk.Toplevel is in a different file





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















how do I close my current window if I can't see the tk.Toplevel(root) from the method I am in?



Here a small example of what i mean:



file1.py:



import Tkinter as tk
import file2
class ExampleMain:
def __init__(self, root):
self.mainFrame = tk.Frame(root)
...
tk.Button(self.mainFrame, command=self.button_pressed)
...

def button_pressed(self):
self.whatever = tk.Toplevel(root)
self.app = file2.ExampleNotMain(self.whatever)

if __name__ == '__main__':
root = tk.Tk()
app = ExampleMain(root)
root.mainloop()


file2.py:



import tkinter as tk
class ExampleNotMain:
def __init__(self, root):
self.frame = tk.Frame(root)
...
tk.Button(self.frame, command=self.close_window)
...

def close_window(self):
=> missing_command_here


In this example I would like to close the second window created (and keep the first).



If all the code is in one file something like



self.whatever.destroy()


would do it. My problem is I can't see the object from the first file with the command being in the second file.



I found something like



execfile("file2.py") 


but I don't like that solution.



Is there a better way to solve my problem?



I would realy apriciate your help.
Thanks in advance.










share|improve this question























  • You code does not show the creation of a second, non-root Toplevel.

    – Terry Jan Reedy
    Nov 16 '18 at 18:59











  • I counted the root as the first window and the non-root window as second. I want to close the non-root window

    – Chris S
    Nov 16 '18 at 19:08













  • I see it now and will answer.

    – Terry Jan Reedy
    Nov 16 '18 at 19:09


















0















how do I close my current window if I can't see the tk.Toplevel(root) from the method I am in?



Here a small example of what i mean:



file1.py:



import Tkinter as tk
import file2
class ExampleMain:
def __init__(self, root):
self.mainFrame = tk.Frame(root)
...
tk.Button(self.mainFrame, command=self.button_pressed)
...

def button_pressed(self):
self.whatever = tk.Toplevel(root)
self.app = file2.ExampleNotMain(self.whatever)

if __name__ == '__main__':
root = tk.Tk()
app = ExampleMain(root)
root.mainloop()


file2.py:



import tkinter as tk
class ExampleNotMain:
def __init__(self, root):
self.frame = tk.Frame(root)
...
tk.Button(self.frame, command=self.close_window)
...

def close_window(self):
=> missing_command_here


In this example I would like to close the second window created (and keep the first).



If all the code is in one file something like



self.whatever.destroy()


would do it. My problem is I can't see the object from the first file with the command being in the second file.



I found something like



execfile("file2.py") 


but I don't like that solution.



Is there a better way to solve my problem?



I would realy apriciate your help.
Thanks in advance.










share|improve this question























  • You code does not show the creation of a second, non-root Toplevel.

    – Terry Jan Reedy
    Nov 16 '18 at 18:59











  • I counted the root as the first window and the non-root window as second. I want to close the non-root window

    – Chris S
    Nov 16 '18 at 19:08













  • I see it now and will answer.

    – Terry Jan Reedy
    Nov 16 '18 at 19:09














0












0








0








how do I close my current window if I can't see the tk.Toplevel(root) from the method I am in?



Here a small example of what i mean:



file1.py:



import Tkinter as tk
import file2
class ExampleMain:
def __init__(self, root):
self.mainFrame = tk.Frame(root)
...
tk.Button(self.mainFrame, command=self.button_pressed)
...

def button_pressed(self):
self.whatever = tk.Toplevel(root)
self.app = file2.ExampleNotMain(self.whatever)

if __name__ == '__main__':
root = tk.Tk()
app = ExampleMain(root)
root.mainloop()


file2.py:



import tkinter as tk
class ExampleNotMain:
def __init__(self, root):
self.frame = tk.Frame(root)
...
tk.Button(self.frame, command=self.close_window)
...

def close_window(self):
=> missing_command_here


In this example I would like to close the second window created (and keep the first).



If all the code is in one file something like



self.whatever.destroy()


would do it. My problem is I can't see the object from the first file with the command being in the second file.



I found something like



execfile("file2.py") 


but I don't like that solution.



Is there a better way to solve my problem?



I would realy apriciate your help.
Thanks in advance.










share|improve this question














how do I close my current window if I can't see the tk.Toplevel(root) from the method I am in?



Here a small example of what i mean:



file1.py:



import Tkinter as tk
import file2
class ExampleMain:
def __init__(self, root):
self.mainFrame = tk.Frame(root)
...
tk.Button(self.mainFrame, command=self.button_pressed)
...

def button_pressed(self):
self.whatever = tk.Toplevel(root)
self.app = file2.ExampleNotMain(self.whatever)

if __name__ == '__main__':
root = tk.Tk()
app = ExampleMain(root)
root.mainloop()


file2.py:



import tkinter as tk
class ExampleNotMain:
def __init__(self, root):
self.frame = tk.Frame(root)
...
tk.Button(self.frame, command=self.close_window)
...

def close_window(self):
=> missing_command_here


In this example I would like to close the second window created (and keep the first).



If all the code is in one file something like



self.whatever.destroy()


would do it. My problem is I can't see the object from the first file with the command being in the second file.



I found something like



execfile("file2.py") 


but I don't like that solution.



Is there a better way to solve my problem?



I would realy apriciate your help.
Thanks in advance.







python tkinter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 18:36









Chris SChris S

182




182













  • You code does not show the creation of a second, non-root Toplevel.

    – Terry Jan Reedy
    Nov 16 '18 at 18:59











  • I counted the root as the first window and the non-root window as second. I want to close the non-root window

    – Chris S
    Nov 16 '18 at 19:08













  • I see it now and will answer.

    – Terry Jan Reedy
    Nov 16 '18 at 19:09



















  • You code does not show the creation of a second, non-root Toplevel.

    – Terry Jan Reedy
    Nov 16 '18 at 18:59











  • I counted the root as the first window and the non-root window as second. I want to close the non-root window

    – Chris S
    Nov 16 '18 at 19:08













  • I see it now and will answer.

    – Terry Jan Reedy
    Nov 16 '18 at 19:09

















You code does not show the creation of a second, non-root Toplevel.

– Terry Jan Reedy
Nov 16 '18 at 18:59





You code does not show the creation of a second, non-root Toplevel.

– Terry Jan Reedy
Nov 16 '18 at 18:59













I counted the root as the first window and the non-root window as second. I want to close the non-root window

– Chris S
Nov 16 '18 at 19:08







I counted the root as the first window and the non-root window as second. I want to close the non-root window

– Chris S
Nov 16 '18 at 19:08















I see it now and will answer.

– Terry Jan Reedy
Nov 16 '18 at 19:09





I see it now and will answer.

– Terry Jan Reedy
Nov 16 '18 at 19:09












1 Answer
1






active

oldest

votes


















0














When you create ExampleNotMain, you pass the 2nd Toplevel self.whatever. In ExampleNotMain.__init__, it gets bound to root. (master or parent would be a better parameter name). In __init__, add self.top = root(or whatever you call the passed-in toplevel). Inclose_window, addself.top.destroy()`.






share|improve this answer
























  • It works. Thanks for your help.

    – Chris S
    Nov 16 '18 at 19:22













  • Then please accept and/or upvote the answer.

    – Terry Jan Reedy
    Nov 16 '18 at 19:31












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343585%2fhow-to-close-a-window-if-tk-toplevel-is-in-a-different-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














When you create ExampleNotMain, you pass the 2nd Toplevel self.whatever. In ExampleNotMain.__init__, it gets bound to root. (master or parent would be a better parameter name). In __init__, add self.top = root(or whatever you call the passed-in toplevel). Inclose_window, addself.top.destroy()`.






share|improve this answer
























  • It works. Thanks for your help.

    – Chris S
    Nov 16 '18 at 19:22













  • Then please accept and/or upvote the answer.

    – Terry Jan Reedy
    Nov 16 '18 at 19:31
















0














When you create ExampleNotMain, you pass the 2nd Toplevel self.whatever. In ExampleNotMain.__init__, it gets bound to root. (master or parent would be a better parameter name). In __init__, add self.top = root(or whatever you call the passed-in toplevel). Inclose_window, addself.top.destroy()`.






share|improve this answer
























  • It works. Thanks for your help.

    – Chris S
    Nov 16 '18 at 19:22













  • Then please accept and/or upvote the answer.

    – Terry Jan Reedy
    Nov 16 '18 at 19:31














0












0








0







When you create ExampleNotMain, you pass the 2nd Toplevel self.whatever. In ExampleNotMain.__init__, it gets bound to root. (master or parent would be a better parameter name). In __init__, add self.top = root(or whatever you call the passed-in toplevel). Inclose_window, addself.top.destroy()`.






share|improve this answer













When you create ExampleNotMain, you pass the 2nd Toplevel self.whatever. In ExampleNotMain.__init__, it gets bound to root. (master or parent would be a better parameter name). In __init__, add self.top = root(or whatever you call the passed-in toplevel). Inclose_window, addself.top.destroy()`.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 19:17









Terry Jan ReedyTerry Jan Reedy

12.3k12140




12.3k12140













  • It works. Thanks for your help.

    – Chris S
    Nov 16 '18 at 19:22













  • Then please accept and/or upvote the answer.

    – Terry Jan Reedy
    Nov 16 '18 at 19:31



















  • It works. Thanks for your help.

    – Chris S
    Nov 16 '18 at 19:22













  • Then please accept and/or upvote the answer.

    – Terry Jan Reedy
    Nov 16 '18 at 19:31

















It works. Thanks for your help.

– Chris S
Nov 16 '18 at 19:22







It works. Thanks for your help.

– Chris S
Nov 16 '18 at 19:22















Then please accept and/or upvote the answer.

– Terry Jan Reedy
Nov 16 '18 at 19:31





Then please accept and/or upvote the answer.

– Terry Jan Reedy
Nov 16 '18 at 19:31




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343585%2fhow-to-close-a-window-if-tk-toplevel-is-in-a-different-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python