Re: [Tutor] events and popup menus

2012-05-05 Thread Chris Hare

Thanks Peter - I finally got back to working on this while my dog was having a 
panic attack from a thunderstorm about 430 AM.  :-)  She is asleep as my feet.  

Anyway, great example and it showed me exactly what i needed to do, AND what I 
was doing wrong.

I appreciate your help!

On May 3, 2012, at 5:30 AM, Peter Otten wrote:

> Chris Hare wrote:
> 
>> I have four images in a frame.  I want to pop up a menu when the user
>> right clicks on an image, and when they choose an option from the menu,
>> execute the action.
>> 
>> I can create the popup menu, and bind it to the image.  However, what I
>> can't figure out is how to detect in the popup menu code which image fired
>> the event so I can do the right thing (like display a larger version of
>> the image, etc.)
>> 
>> # create a menu
>> self.popup = Menu(self.pictureWindow, tearoff=0)
>> self.popup.add_command(label="Change Picture",
>> command=self.selectPicture) self.popup.add_command(label="Make Primary",
>> command=self.selectPicture) self.popup.add_command(label="Large View",
>> command=self.selectPicture)
> 
> You should have a different callback for every menu item:
> 
> self.popup.add_command(label="Change Picture", command=self.change_picture)
> ...
> self.popup.add_command(label="Large View", command=self.large_view)
> 
> 
>> self.picture1.bind("", self.do_popup)
>> 
>> def do_popup(self,event):
>># display the popup menu
>>  try:
>>   self.popup.tk_popup(event.x_root, event.y_root, 0)
>> 
>> finally:
>> # make sure to release the grab (Tk 8.0a1 only)
>> self.popup.grab_release()
>> 
>> Thanks for the advice!
> 
> You can remember the widget from do_popup()'s event argument
> 
> def do_popup(self, event):
>self.current_picture = event.widget
>...
> 
> and later refer to it in the menu callbacks 
> 
> def select_picture(self):
>picture = self.current_picture
>...
> 
> I got a bit distracted struggling with PIL, therefore my "self-contained 
> demo" got rather baroque. You may still find it useful:
> 
> $ cat tk_popup_demo.py
> import sys
> import Tkinter as tk
> import ImageTk
> import Image
> 
> current_label = None
> 
> def do_popup(event):
>global current_label
>current_label = event.widget
>try:
>popup.tk_popup(event.x_root, event.y_root, 0)
>finally:
>popup.grab_release()
> 
> def rotate_picture():
>image = current_label.photoimage.image.rotate(90)
>photoimage = ImageTk.PhotoImage(image)
>photoimage.image = image
>current_label.photoimage = current_label["image"] = photoimage
> 
> def flip_picture():
>print "flip picture"
> 
> def load_image(filename, maxsize=(500, 500), padcolor="#f80"):
>image = Image.open(filename)
>image.thumbnail(maxsize)
>if image.size != maxsize:
>padded_image = Image.new(image.mode, maxsize, color=padcolor)
>maxx, maxy = maxsize
>x, y = image.size
>padded_image.paste(image, ((maxx-x)//2, (maxy-y)//2))
>image = padded_image
>assert image.size == maxsize
>return image
> 
> root = tk.Tk()
> 
> picturefiles = sys.argv[1:4]
> for i, filename in enumerate(picturefiles):
>image = load_image(filename)
>photoimage = ImageTk.PhotoImage(image)
>photoimage.image = image
> 
>label = tk.Label(root, image=photoimage)
>label.photoimage = photoimage
>label.grid(row=0, column=i)
>label.bind("", do_popup)
> 
> popup = tk.Menu(root, tearoff=0)
> popup.add_command(label="Rotate", command=rotate_picture)
> popup.add_command(label="Flip", command=flip_picture)
> 
> root.mainloop()
> 
> Invoke with a few picture names (all but the first three will be ignored):
> 
> $ python tk_popup_demo.py *.jpg
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] A simple "if" and "elif" problem

2012-05-05 Thread Santosh Kumar
I am reading the documentation and I'm in the section 4.1. Let me
write it down here:

>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
...  x = 0
...  print('Negative changed to zero')
... elif x == 0:
...  print('Zero')
... elif x == 1:
...  print('Single')
... else:
...  print('More')
...
More

Now I want to add a filter in this script, I want when a user enter a
string here it give a warning "Please enter a number like 0 or 2".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A simple "if" and "elif" problem

2012-05-05 Thread Emile van Sebille

On 5/5/2012 11:01 AM Santosh Kumar said...

I am reading the documentation and I'm in the section 4.1. Let me
write it down here:


You'll need to peek ahead to section 8 Errors and Exceptions.

Try and see if that doesn't get you going.

Emile






x = int(input("Please enter an integer: "))

Please enter an integer: 42

if x<  0:

...  x = 0
...  print('Negative changed to zero')
... elif x == 0:
...  print('Zero')
... elif x == 1:
...  print('Single')
... else:
...  print('More')
...
More

Now I want to add a filter in this script, I want when a user enter a
string here it give a warning "Please enter a number like 0 or 2".
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A simple "if" and "elif" problem

2012-05-05 Thread xgermx
Python novice here. Caveat emptor.

Since all python input is read in as a string, you could try checking
to see if the value is a digit with the .isdigit string method.
e.g.
 if userinput.isdigit():
   #do stuff
else
  #more stuff

http://stackoverflow.com/questions/5424716/python-how-to-check-if-input-is-a-number-given-that-input-always-returns-stri
http://docs.python.org/library/stdtypes.html

On Sat, May 5, 2012 at 1:08 PM, Emile van Sebille  wrote:
> On 5/5/2012 11:01 AM Santosh Kumar said...
>
>> I am reading the documentation and I'm in the section 4.1. Let me
>> write it down here:
>
>
> You'll need to peek ahead to section 8 Errors and Exceptions.
>
> Try and see if that doesn't get you going.
>
> Emile
>
>
>
>
>>
> x = int(input("Please enter an integer: "))
>>
>> Please enter an integer: 42
>
> if x<  0:
>>
>> ...      x = 0
>> ...      print('Negative changed to zero')
>> ... elif x == 0:
>> ...      print('Zero')
>> ... elif x == 1:
>> ...      print('Single')
>> ... else:
>> ...      print('More')
>> ...
>> More
>>
>> Now I want to add a filter in this script, I want when a user enter a
>> string here it give a warning "Please enter a number like 0 or 2".
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A simple "if" and "elif" problem

2012-05-05 Thread Alan Gauld

On 05/05/12 19:01, Santosh Kumar wrote:



x = int(input("Please enter an integer: "))

Please enter an integer: 42



Now I want to add a filter in this script, I want when a user enter a
string here it give a warning "Please enter a number like 0 or 2".


What happens if the user does not enter a valid integer
at the moment? You should get an error message?

That error message will provide the name of a particular type of Error.
Python lets you catch errors and handle them. You will find out how to 
do that later in your tutorial.


Once you catch the error you will likely want to loop around and ask for 
more input. You will need a loop - probably a while loop - to do that.


So in pseudo code you will wind up with something like:

while True:
  try:
read the input and convert to int
break out of the loop
  except when there is an error:
print a message and go round the loop again

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor