Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64038
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Bind event is giving me a bug. |
| Date | 2014-01-15 20:44 -0500 |
| References | <803c85f0-2f0f-4fc9-b043-710a77648546@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5560.1389836693.18130.python-list@python.org> (permalink) |
On 1/15/2014 3:16 PM, eneskristo@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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Bind event is giving me a bug. eneskristo@gmail.com - 2014-01-15 12:16 -0800 Re: Bind event is giving me a bug. Peter Otten <__peter__@web.de> - 2014-01-15 21:59 +0100 Re: Bind event is giving me a bug. MRAB <python@mrabarnett.plus.com> - 2014-01-15 21:10 +0000 Re: Bind event is giving me a bug. eneskristo@gmail.com - 2014-01-15 13:19 -0800 Re: Bind event is giving me a bug. Peter Otten <__peter__@web.de> - 2014-01-15 22:25 +0100 Re: Bind event is giving me a bug. Terry Reedy <tjreedy@udel.edu> - 2014-01-15 20:44 -0500
csiph-web