Bind event is giving me a bug.

E

eneskristo

While working with tkinter in python 3.3, I had the following problem.
def get_text(event):
self.number_of_competitors = entered_text.get()
try:
self.number_of_competitors = int(self.number_of_competitors)
except:
pass
if type(self.number_of_competitors) == int:
root.destroy()
else:
label.config(text = "Enter the number of competitors. Please enter a number.")
root = Tk()
label = Label(root, text = "Enter the number of competitors.")
label.pack(side = TOP)
entered_text = Entry(root)
entered_text.pack()
Button(root, text = "Submit", command = get_text).pack()
root.bind('<Enter>', get_text)
root.mainloop()

This is a buggy part of the code. When I run it, instead of doing what it should do, it responds to all events BUT enter. I'm not sure if this error is on tkinters or my side. Please help!
 
P

Peter Otten

While working with tkinter in python 3.3, I had the following problem.
root = Tk()
label = Label(root, text = "Enter the number of competitors.")
label.pack(side = TOP)
entered_text = Entry(root)
entered_text.pack()
Button(root, text = "Submit", command = get_text).pack()
root.bind('<Enter>', get_text)

Quoting http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/event-types.html
"""
Enter
The user moved the mouse pointer into a visible part of a widget. (This is
different than the enter key, which is a KeyPress event for a key whose name
is actually 'return'.)
"""
 
M

MRAB

While working with tkinter in python 3.3, I had the following problem.
def get_text(event):
self.number_of_competitors = entered_text.get()
try:
self.number_of_competitors = int(self.number_of_competitors)

A bare except like this is virtually never a good idea:
except:
pass
if type(self.number_of_competitors) == int:
root.destroy()
else:
label.config(text = "Enter the number of competitors. Please enter a number.")

Something like this would be better:

try:
self.number_of_competitors = int(entered_text.get())
except ValueError:
label.config(text="Enter the number of competitors. Please
enter a number.")
else:
root.destroy()
root = Tk()
label = Label(root, text = "Enter the number of competitors.")
label.pack(side = TOP)
entered_text = Entry(root)
entered_text.pack()

This will make it call 'get_text' when the button is clicked:
Button(root, text = "Submit", command = get_text).pack()

This will make it call 'get_text' when the pointer enters the frame:
 
E

eneskristo

Thank you, I thought Enter was Enter, but I still have this problem, when I press the Button, this appears:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
TypeError: get_text() missing 1 required positional argument: 'event'
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
TypeError: get_text() missing 1 required positional argument: 'event'

Should I make 2 functions, or is there a simpler solution?
 
P

Peter Otten

MRAB said:
This will make it call 'get_text' when the button is clicked:

....and then produce a TypeError because of the missing `event` argument. To
avoid that you can provide a default with

def get_text(event=None):
...
 
T

Terry Reedy

While working with tkinter in python 3.3, I had the following problem.

Please paste working code that people can experiment with.

from tkinter import *
def get_text(event):

If this were a method, (which the indent of the body suggests it once
was) it would have to have a 'self' parameter, and you would have to
bind a bound method.
self.number_of_competitors = entered_text.get()

Since it is just a function, and has no 'self' parameter, this raises
NameError. I condensed the function to

try:
int(entered_text.get())
root.destroy()
except ValueError:
label.config(text = "Enter the number of competitors.
Please enter a number.")
try:
self.number_of_competitors = int(self.number_of_competitors)
except:

Bare excepts are bad.
pass
if type(self.number_of_competitors) == int:
root.destroy()
else:
label.config(text = "Enter the number of competitors. Please enter a number.")
root = Tk()
label = Label(root, text = "Enter the number of competitors.")
label.pack(side = TOP)
entered_text = Entry(root)

Since Entry only allows one line, I would have thought that it should
take a command=func option invoked by \n. Instead, it seems to swallow
newlines.
entered_text.pack()
Button(root, text = "Submit", command = get_text).pack()

As near as I can tell, the Button button-press event in *not* bound to
get_text but to a fixed event handler that calls get_text *without* an
argument.
root.bind('<Enter>', get_text)

This does bind to an event so that it does call with an event arg. I
just removed this and the window acts as it should.

Since get_event ignores event, event=None should make it work either
way. However, when I try that, the window disappears without being
touched, as if \n is randomly generated internally. So I would say to
skip this until you know more than I do.
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top