[newbie] problem trying out simple non object oriented use of Tkinter

Jean Dubois jeandubois314 at gmail.com
Fri Dec 6 08:12:01 EST 2013


Op vrijdag 6 december 2013 13:30:53 UTC+1 schreef Daniel Watkins:
> Hi Jean,
> 
> 
> 
> On Fri, Dec 06, 2013 at 04:24:59AM -0800, Jean Dubois wrote:
> 
> > I'm trying out Tkinter with the (non object oriented) code fragment below:
> 
> > It works partially as I expected, but I thought that pressing "1" would
> 
> > cause the program to quit, however I get this message:
> 
> > TypeError: quit() takes no arguments (1 given), I tried changing quit to quit()
> 
> > but that makes things even worse. So my question: can anyone here help me
> 
> > debug this?
> 
> 
> 
> I don't know the details of the Tkinter library, but you could find out
> 
> what quit is being passed by modifying it to take a single parameter and
> 
> printing it out (or using pdb):
> 
> 
> 
>     def quit(param):
> 
>         print(param)
> 
>         sys.exit()
> 
> 
> 
> Having taken a quick look at the documentation, it looks like event
> 
> handlers (like your quit function) are passed the event that triggered
> 
> them.  So you can probably just ignore the parameter:
> 
> 
> 
>     def quit(_):
> 
>         sys.exit()
> 
> 
> 
> 
> 
> Cheers,
> 
> 
> 
> Dan

I tried out your suggestions and discovered that I had the line
import sys to the program. So you can see below what I came up with.
It works but it's not all clear to me. Can you tell me what "label.bind("<1>", quit)" is standing for? What's the <1> meaning?



#!/usr/bin/env python
import Tkinter as tk
import sys
#underscore is necessary in the following line
def quit(_):
    sys.exit()
root = tk.Tk()
label = tk.Label(root, text="Click mouse here to quit")
label.pack()
label.bind("<1>", quit)
root.mainloop()

thanks
jean





More information about the Python-list mailing list