Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64014 > unrolled thread

Bind event is giving me a bug.

Started byeneskristo@gmail.com
First post2014-01-15 12:16 -0800
Last post2014-01-15 20:44 -0500
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#64014 — Bind event is giving me a bug.

Fromeneskristo@gmail.com
Date2014-01-15 12:16 -0800
SubjectBind event is giving me a bug.
Message-ID<803c85f0-2f0f-4fc9-b043-710a77648546@googlegroups.com>
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!

[toc] | [next] | [standalone]


#64016

FromPeter Otten <__peter__@web.de>
Date2014-01-15 21:59 +0100
Message-ID<mailman.5543.1389819558.18130.python-list@python.org>
In reply to#64014
eneskristo@gmail.com wrote:

> 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'.)
"""

So I think you want "<Return>", not "<Enter>".

> 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!

[toc] | [prev] | [next] | [standalone]


#64018

FromMRAB <python@mrabarnett.plus.com>
Date2014-01-15 21:10 +0000
Message-ID<mailman.5545.1389820248.18130.python-list@python.org>
In reply to#64014
On 2014-01-15 20:16, eneskristo@gmail.com wrote:
> 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:

> root.bind('<Enter>', get_text)

Did you mean '<Return>', i.e. the Return key?

> 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!
>

[toc] | [prev] | [next] | [standalone]


#64019

Fromeneskristo@gmail.com
Date2014-01-15 13:19 -0800
Message-ID<c14bb5bd-2f01-47c5-8d40-795f12dffb13@googlegroups.com>
In reply to#64014
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?

[toc] | [prev] | [next] | [standalone]


#64020

FromPeter Otten <__peter__@web.de>
Date2014-01-15 22:25 +0100
Message-ID<mailman.5546.1389821146.18130.python-list@python.org>
In reply to#64014
MRAB wrote:

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

...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):
    ...

[toc] | [prev] | [next] | [standalone]


#64038

FromTerry Reedy <tjreedy@udel.edu>
Date2014-01-15 20:44 -0500
Message-ID<mailman.5560.1389836693.18130.python-list@python.org>
In reply to#64014
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web