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


Groups > comp.lang.python > #64018

Re: Bind event is giving me a bug.

Date 2014-01-15 21:10 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: Bind event is giving me a bug.
References <803c85f0-2f0f-4fc9-b043-710a77648546@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.5545.1389820248.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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