tkinter wm_delete_window

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yvesd

    tkinter wm_delete_window

    hello i want to intercept tkinter python system events like
    wm_delete_windo w
    and if possible for any window, but the newest code I've produced give
    me
    an error :

    Traceback (most recent call last):
    File "C:\Documen ts and Settings\yvesd\ Bureau\protowin .py", line 36,
    in ?
    b1 = Tkinter.Button (win1)
    File "C:\Python24\li b\lib-tk\Tkinter.py", line 1933, in __init__
    Widget.__init__ (self, master, 'button', cnf, kw)
    File "C:\Python24\li b\lib-tk\Tkinter.py", line 1856, in __init__
    BaseWidget._set up(self, master, cnf)
    File "C:\Python24\li b\lib-tk\Tkinter.py", line 1834, in _setup
    self.tk = master.tk
    AttributeError: MyDlg instance has no attribute 'tk'

    thanks a lot for the help answer.

    here is my code :

    import Tkinter
    from Tkinter import *

    class MyDlg(Toplevel) :


    def ma_fonction():
    print "my function is finished."


    def __init__(arg1, arg2):
    arg1.protocol(" WM_DELETE_WINDO W", arg1.ma_fonctio n)

    root = Tkinter.Tk ()
    #win1 = Tkinter.Topleve l (root)
    win1 = MyDlg(root)
    b1 = Tkinter.Button (win1)
    b1.config (text="hello")

  • Eric Brunel

    #2
    Re: tkinter wm_delete_windo w

    On Tue, 18 Jul 2006 11:16:04 +0200, yvesd <yves.delalande @gmail.comwrote :
    hello i want to intercept tkinter python system events like
    wm_delete_windo w
    and if possible for any window, but the newest code I've produced give
    me
    an error :
    Traceback (most recent call last):
    File "C:\Documen ts and Settings\yvesd\ Bureau\protowin .py", line 36,
    in ?
    b1 = Tkinter.Button (win1)
    File "C:\Python24\li b\lib-tk\Tkinter.py", line 1933, in __init__
    Widget.__init__ (self, master, 'button', cnf, kw)
    File "C:\Python24\li b\lib-tk\Tkinter.py", line 1856, in __init__
    BaseWidget._set up(self, master, cnf)
    File "C:\Python24\li b\lib-tk\Tkinter.py", line 1834, in _setup
    self.tk = master.tk
    AttributeError: MyDlg instance has no attribute 'tk'
    thanks a lot for the help answer.
    here is my code :
    import Tkinter
    from Tkinter import *
    Do not import the same module twice: either use "import Tkinter" and
    prefix everything in it with 'Tkinter.', or use "from Tkinter import *"
    and don't specify any prefix. You're mixing both here.
    class MyDlg(Toplevel) :
    def ma_fonction():
    You must specify 'self' as first parameter here. Or put 'ma_fonction'
    outside the class.
    print "my function is finished."
    def __init__(arg1, arg2):
    arg1.protocol(" WM_DELETE_WINDO W", arg1.ma_fonctio n)
    Please don't use anything else than 'self' as the name for methods' first
    parameter. It will confuse everyone who knows Python. And your mistake is
    here: the constructor for the super-class (Toplevel) is not called
    automatically by the sub-class constructor; so you have to do it yourself.
    So just rewrite these two lines as:

    def __init__(self):
    Toplevel.__init __(self)
    self.protocol(" WM_DELETE_WINDO W", self.ma_fonctio n)

    and everything should work fine. BTW, I've removed the parameter arg2 that
    you didn't use at all.
    root = Tkinter.Tk ()
    #win1 = Tkinter.Topleve l (root)
    win1 = MyDlg(root)
    Replace that with:

    win1 = MyDlg()
    b1 = Tkinter.Button (win1)
    b1.config (text="hello")
    An additional small note: you can write the last two lines as a single one:

    b1 = Tkinter.Button( win1, text='hello')

    And you also must call:
    - b1.pack, b1.grid or b1.place to put your button somewhere in your window.
    - root.mainloop() in the end or your window will never appear.

    If you want to do Tkinter the OO way, you may also create the button in
    the dialog's constructor.

    So here would be my version of your program:

    ----------------------------------------------
    from Tkinter import *

    class MyDlg(Toplevel) :
    def __init__(self):
    Toplevel.__init __(self)
    b1 = Button(self, text='hello')
    b1.pack()
    self.protocol(" WM_DELETE_WINDO W", self.ma_fonctio n)
    def ma_fonction(sel f):
    print "my function is finished."

    root = Tk()
    win1 = MyDlg()
    root.mainloop()
    ----------------------------------------------

    HTH
    --
    python -c "print ''.join([chr(154 - ord(c)) for c in
    'U(17zX(%,5.zmz 5(17l8(%,5.Z*(9 3-965$l7+-'])"

    Comment

    Working...