-2

I can't figure out how to call a function from a tkinter Frame. I'm trying to implement a method that would protect the information from the App in case it's closed unintentionally. Could someone please help ?

from tkinter import Tk, Frame, Label

class MyApp(Tk):
    def __init__(self, *args, **kwargs):  
        Tk.__init__(self, *args, **kwargs)
        container = Frame(self)
        container.pack(side="top", fill="both", expand = True)

        frame = StartPage(container, self)
        frame.grid(row=0, column=0, sticky='nsew')
        frame.tkraise()

class StartPage(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self,parent)
        Label(self, text="close this window from the X in the corner").grid(row=1,column=1)

    def Stop(self):                                    
        print('exiting')

app = MyApp()

if __name__=='__main__':
    try:
        app.mainloop()
    finally:
        OpenApp = MyApp()
        OpenClass = StartPage(parent, OpenApp)
        OpenClass.Stop()

I can't call the "Stop" function when closing tkinter. I get a NameError: name 'parent' is not defined. What should I put instead of "parent" ? If using the root.protocol("WM_DELETE_WINDOW", root.iconify) the function inside the class still has to be called.

qode
  • 43
  • 7
  • Show us the code that generates the error, and show the actual and complete error. Right now, nothing in your code is calling `Stop`. – Bryan Oakley Dec 22 '17 at 02:27
  • the "Stop" is initiated when the tkinter frame is closed by pressing the "X" in it's corner. I'm trying to implement a method that would protect the App when is being closed unintentionally. – qode Dec 22 '17 at 02:33
  • Please show what you've tried. – Bryan Oakley Dec 22 '17 at 02:50
  • 1
    Does the following link answer your question? https://stackoverflow.com/q/111155/7432 – Bryan Oakley Dec 22 '17 at 02:51
  • Hi Bryan, using the root.protocol still implies calling that "Stop" function from outside that class. – qode Dec 22 '17 at 03:13
  • The following script creates a tkinter frame. When closing the tkinter manually by pressing the "X" in window's corner, an error is generated, triggered by the line "OpenClass.Stop()". The "Stop" function from inside the "StartPage" class is not being initiated. – qode Dec 22 '17 at 03:22
  • 1
    There's nothing special to call a function outside the class. Like with any class, you need a reference to the instance. Tkinter doesn't require anything out of the ordinary. – Bryan Oakley Dec 22 '17 at 03:39

1 Answers1

1

Try changing the line:

OpenClass = StartPage(parent, OpenApp)

to:

OpenClass = StartPage(OpenApp, OpenApp)

class StartPage(Frame):
        def __init__(self, parent, controller):

Requires you to pass parent argument which you're trying to pass a reference never created named parent.

Note that in tkinter apps parent is usually another widget that contains the children.

Nae
  • 14,209
  • 7
  • 52
  • 79
  • Note that creating an MCVE helped enormously as it took me like a minute to see what OP's issue is after the edit. – Nae Dec 22 '17 at 04:23
  • @qode No problem. See [WM_DELETE_WINDOW](http://example.com) for more standard way of handling close events though. – Nae Dec 22 '17 at 04:32