Bind event is giving me a bug.
Terry Reedy
tjreedy at udel.edu
Wed Jan 15 20:44:32 EST 2014
On 1/15/2014 3:16 PM, eneskristo at gmail.com wrote:
> 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.
> 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!
--
Terry Jan Reedy
More information about the Python-list
mailing list