Calling function from another module

Discussion in 'Python' started by craf, Dec 16, 2010.

  1. craf

    craf Guest

    Hi.

    The query code is as follows:

    ------------------------------------------------------
    import Tkinter
    import tkMessageBox


    class App:
    def __init__(self, master):
    master.protocol("WM_DELETE_WINDOW",quit)


    def quit():
    if tkMessageBox.askyesno('','Exit'):
    master.quit()


    master =Tkinter.Tk()
    app = App(master)
    master.mainloop()
    -------------------------------------------------------

    As you can see, when I run and close the main window displays
    a text box asking if you want to quit, if so, closes
    application.

    Question:

    Is it possible to define the quit() function in another separate
    module?.
    I tried it, but it throws the error that the global name
    'master' is not defined.

    Thanks in advance.

    Regards

    Cristian Abarzúa
    craf, Dec 16, 2010
    #1
    1. Advertising

  2. craf

    rantingrick Guest

    On Dec 15, 6:46 pm, craf <> wrote:
    > Hi.
    >
    > The query code is as follows:
    >
    > ------------------------------------------------------
    > import Tkinter
    > import tkMessageBox
    >
    > class App:
    >     def __init__(self, master):
    >         master.protocol("WM_DELETE_WINDOW",quit)
    >
    > def quit():
    >     if tkMessageBox.askyesno('','Exit'):
    >         master.quit()
    >
    > master =Tkinter.Tk()
    > app = App(master)
    > master.mainloop()
    > -------------------------------------------------------
    >
    > As you can see, when I run and close the main window displays
    > a text box asking if you want to quit, if so, closes
    > application.
    >
    > Question:
    >
    > Is it possible to define the quit() function in another separate
    > module?.
    > I tried it, but it throws the error that the global name
    > 'master' is not defined.


    Please explain in detail what the "other module" is doing. And as
    written this class "app" looks pretty useless to me. Why subclass Tk
    (well it appears you "tried" to subclass it anyway) just to override
    capabilities that are already available within Tk?

    Also i see many mistakes in this very small code sample. Using my
    deductive logic leads me to believe that many mistakes in a small code
    sample translates to enormous mistakes in a larger code base. So feel
    free to ramble incessantly about the intricate workings of this jewel
    of Python scripting you are soon to drop on this malnourished group of
    coders.
    rantingrick, Dec 16, 2010
    #2
    1. Advertising

  3. craf

    Peter Otten Guest

    craf wrote:

    > Hi.
    >
    > The query code is as follows:
    >
    > ------------------------------------------------------
    > import Tkinter
    > import tkMessageBox
    >
    >
    > class App:
    > def __init__(self, master):
    > master.protocol("WM_DELETE_WINDOW",quit)
    >
    >
    > def quit():
    > if tkMessageBox.askyesno('','Exit'):
    > master.quit()
    >
    >
    > master =Tkinter.Tk()
    > app = App(master)
    > master.mainloop()
    > -------------------------------------------------------
    >
    > As you can see, when I run and close the main window displays
    > a text box asking if you want to quit, if so, closes
    > application.
    >
    > Question:
    >
    > Is it possible to define the quit() function in another separate
    > module?.
    > I tried it, but it throws the error that the global name
    > 'master' is not defined.


    You can have the modules import each other and then access the master as
    <module>.master where you'd have to replace <module> with the actual name of
    the module, but that's a bad design because

    (1) you create an import circle
    (2) functions relying on global variables already are a bad idea

    Your other option is to pass 'master' explicitly and then wrap it into a
    lambda function (or functools.partial):

    $ cat tkquitlib.py
    import tkMessageBox

    def quit(master):
    if tkMessageBox.askyesno('','Exit'):
    master.quit()


    $ cat tkquit_main.py
    import Tkinter

    import tkquitlib

    class App:
    def __init__(self, master):
    master.protocol("WM_DELETE_WINDOW", lambda: tkquitlib.quit(master))

    master = Tkinter.Tk()
    app = App(master)
    master.mainloop()

    Peter
    Peter Otten, Dec 16, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Lagemann
    Replies:
    8
    Views:
    452
    Mike Wahler
    Jan 10, 2005
  2. tiwy
    Replies:
    0
    Views:
    423
  3. Satish Chimakurthi

    Function calling another function

    Satish Chimakurthi, Mar 1, 2004, in forum: Python
    Replies:
    2
    Views:
    354
    Satish Chimakurthi
    Mar 1, 2004
  4. Jon Clements
    Replies:
    3
    Views:
    300
    geremy condra
    Aug 6, 2010
  5. dutzend
    Replies:
    1
    Views:
    78
Loading...

Share This Page