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

J

Jean Dubois

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?

#!/usr/bin/env python
import Tkinter as tk
def quit():
sys.exit()
root = tk.Tk()
label = tk.Label(root, text="Hello, world")
label.pack()
label.bind("<1>", quit)
root.mainloop()

p.s. I like the code not object orientated
 
D

Daniel Watkins

Hi Jean,

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
 
J

Jean Dubois

Op vrijdag 6 december 2013 13:30:53 UTC+1 schreef Daniel Watkins:
Hi Jean,












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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,818
Messages
2,569,732
Members
45,691
Latest member
Dick331194

Latest Threads

Top