Re: [Tutor] Tkinter: no module named messagebox (brandon w)

2011-08-14 Thread Robert Sjoblom
> I have tried to follow the tutorial I found here:
>
> Python 2.7 Tutorial
> http://www.youtube.com/watch?v=uh6AdDX7K7U
>
> This is what I have done so far:
>
> #!/usr/bin/python
>
> from Tkinter import *
> import Tkinter.MessageBox

I figured I might as well, given how I recently had to learn about
this, give you a heads up on imports. The format "from module import *
" is not a very good idea: it makes your code harder to read (as the
reader has to inspect each and every barename to check whether it's
actually assigned locally OR comes from the deuced *). To quote the
Zen of Python; "namespaces are a honking great idea -- let's do more
of those!".

You can import modules with several different syntaxes:
import importable
import importable1, importable2, ..., importableN
import importable as preferred_name

Here importable is usually a module such as collections, but could be
a package or module in a package, in which case each part is separated
with a dot, for example os.path. The first two syntaxes are the
simplest  and also the safest because they avoid the possibility of
having name conflicts, since they force us to always use fully
qualified names.

The third syntax allows us to give a name of our choice to the package
or module we are importing. Theoretically, this could lead to name
clashes, but in practice the "import importable as" syntax is used to
avoid them.

There are some other import syntaxes:
from importable import object as preferred_name
from importable import object1, object2, ..., objectN
from importable import (object1, object2, ..., objectN)
from importable import *

In the last syntax, the * means "import everything that is not
private", which in practical terms means either that every object in
the module is imported except for those whose names begin with a
leading underscore, or, if the module has a global __all__ variable
that holds a list of names, that all the objects in the __all__
variable are imported.

The from importable import * syntax imports all the objects from the
module (or all the modules from the package) -- this could be hundreds
of names. In the case of from os.path import *, almost 40 names are
imported, including "dirname", "exists", and "split", any of which
might be names we would prefer to use for our own variables or
functions.

For example, if we write
from os.path import dirname
we can conveniently call dirname() without qualification. But if
further on in our code we write
dirname = "."
the object reference dirname will now be bound to the string "."
instead of to the dirname() function, so if we try calling dirname()
we will get a TypeError exception because dirname now refers to a
string, and we can't call strings.

However, given that Tkinter is such a huge package to begin with, I'd
say that you should continue to use from Tkinter import *, but be
aware of what you're doing when you type that, and that there is a
certain risk of conflicts.

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


Re: [Tutor] Tkinter: no module named messagebox (brandon w)

2011-08-14 Thread Wayne Werner
On Aug 14, 2011 2:12 AM, "Robert Sjoblom"  wrote:
>
> However, given that Tkinter is such a huge package to begin with, I'd
> say that you should continue to use from Tkinter import *, but be
> aware of what you're doing when you type that, and that there is a
> certain risk of conflicts.

Also, most (all?) Tkinter names are uppercase, such as Button, Label, etc.

Of course I personally I usually do

import Tkinter as tk

Which means I only have to type 3 extra characters, but it removes any
ambiguity - tk.Something had to come from Tkinter, and never something that
I defined.

I suppose it's really all about trade-offs, and what you find works best, or
whatever the prevailing convention is when you're working with others.

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


[Tutor] python

2011-08-14 Thread je.rees e-mail
I have made a small program but I would like to know how to write or. There
is multiple choice answer.
Good=raw_input("Good to hear")
ok=raw_input("Good good")
Bad=raw_input("Oh dear")
I would only like the person using the program to be able to pick one.

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


Re: [Tutor] Tkinter: no module named messagebox (brandon w)

2011-08-14 Thread Alan Gauld

On 14/08/11 14:07, Wayne Werner wrote:

Of course I personally I usually do

import Tkinter as tk

Which means I only have to type 3 extra characters, but it removes any
ambiguity - tk.Something had to come from Tkinter


Thats exactly what I tend to do nowadays.

I used to use the import * method for tkinter but eventually
I got bit by a name clash and it took too long to realize the
problem so I now use the "import as tk" shortcut except for
very short scripts - usually just for tests or experiments..

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


[Tutor] (no subject)

2011-08-14 Thread je.rees e-mail
I would like this script to only have a choice of one these. I'm not sure
how to do it. My example would be when someone types Good next to print how
I want it to reply Thats nice. Anyone know how and if I am making more
mistakes point them out please.


How=("How are you today: ")
print How
Good=('Thats nice')
good=raw_input(Good)
Good=raw_input(Good)
Ok=('Good good')
Ok=raw_input(Ok)
ok=raw_input(Ok)
Bad=('Oh dear')
Bad=raw_input(Bad)
bad=raw_input(Bad)

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


Re: [Tutor] (no subject)

2011-08-14 Thread Steven D'Aprano

je.rees e-mail wrote:

I would like this script to only have a choice of one these. I'm not sure
how to do it. My example would be when someone types Good next to print how
I want it to reply Thats nice. Anyone know how and if I am making more
mistakes point them out please.



Consider this example:


def test():
prompt = "Please type 'Thats nice'"
answer = raw_input(prompt)
if answer == "Thats nice":
print "You have typed it accurately"
else:
print "I'm sorry, you have made a mistake"


Paste that into the interactive interpreter, then run:

test()


and follow the instructions. Does that help?



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


Re: [Tutor] python

2011-08-14 Thread brandon w

On 08/14/2011 09:04 AM, je.rees e-mail wrote:
I have made a small program but I would like to know how to write or. 
There is multiple choice answer.

Good=raw_input("Good to hear")
ok=raw_input("Good good")
Bad=raw_input("Oh dear")
I would only like the person using the program to be able to pick one.

Thanks


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

Why not writing the program and running it to see what happens?

try something like this:

good = raw_input("Did you have a nice day? ")

if good == "yes":
print "I'm glad you had a nice day."
else:
print "Sorry to hear that."
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python

2011-08-14 Thread Jack Trades
On Sun, Aug 14, 2011 at 8:04 AM, je.rees e-mail  wrote:

> I have made a small program but I would like to know how to write or. There
> is multiple choice answer.
> Good=raw_input("Good to hear")
> ok=raw_input("Good good")
> Bad=raw_input("Oh dear")
> I would only like the person using the program to be able to pick one.
>
> Thanks
>

You might want to try something like this...

def ask():
  ans = raw_input("Please enter (g)ood, (o)k or (b)ad: ")

  if ans == "g" or ans == "good":
print "Good to hear"
  elif ans == "o" or ans == "ok":
print "Good good"
  elif ans == "b" or ans == "bad":
print "Oh dear"
  else:
print "Incorrect choice"

ask()

And here's how it works...

>>> ask()
Please enter (g)ood, (o)k or (b)ad: b
Oh dear
>>> ask()
Please enter (g)ood, (o)k or (b)ad: o
Good good
>>> ask()
Please enter (g)ood, (o)k or (b)ad: ok
Good good

Or you could also use something a bit more general like this...

def ask(question, list_of_responses):
  ans = raw_input(question)
  return list_of_responses[int(ans)]

>>> ask("(0)Good (1)OK (2)Bad: ", ["Good to hear", "Good good", "Oh dear"])
(0)Good (1)OK (2)Bad: 0
'Good to hear'


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


[Tutor] Moveable and Animated Sprites

2011-08-14 Thread Jordan
Is there a more Pythonic way to create the class
"MoveableAnimatedSheetSprite" I am using multiple inheritance but I see
that I am really loading the image twice when I call __init__ for
"AnimatedSheetSprite" and "MoveableSprite". Should I just make a
moveable class and have moveable items inherit it with out inheriting
from "Sprite"?
By the way the code does work as intended right now.
Thank you in advance, if you need more code or what ever just let me know.

import pygame
from vec2d import *
class Sprite(pygame.sprite.Sprite):
"""This defines a sprite that is a single image in a file that only
contains
it as an image."""
def __init__(self, imageName, startx=0, starty=0):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(imageName)
self.rect=self.image.get_rect()
self.image.convert()
self.x=startx
self.y=starty
self.position=vec2d(self.x,self.y)
self.selected=False  

def update(self):
pygame.sprite.Sprite.update(self)

class AnimatedSheetSprite(Sprite):
"""This defines a sprite that is multiple images but is in a image
file that
has only it's images."""
def __init__(self, imageName, startx=0, starty=0, height=0, width=0):
Sprite.__init__(self, imageName, startx, starty)
self.images=[]
masterWidth, masterHeight=self.image.get_size()
for i in xrange(int(masterWidth/width)):
   
self.images.append(self.image.subsurface((i*width,0,width,height)))
self.image=self.images[0]
self.maxFrame=len(self.images)-1
self.frame=0

def animate(self):
if self.frame < self.maxFrame:
self.frame=self.frame+1 
else:
self.frame=0
self.image=self.images[self.frame]
   
def update(self):
Sprite.update(self)
self.animate()

class MoveableSprite(Sprite):
"""This defines a sprite that is a single image in a file that only
contains
it as an image. But this sprite can move."""
def __init__(self, imageName, startx=0, starty=0, targetx=0,
targety=0, speed=2):
Sprite.__init__(self, imageName, startx, starty)
self.target=vec2d(targetx,targety)
self.speed=speed

def move(self):
self.direction=self.target-self.position
if self.direction.length > self.speed:
self.position= self.position+self.speed
   
def update(self):
Sprite.update(self)
self.move()
   
class MoveableAnimatedSheetSprite(MoveableSprite, AnimatedSheetSprite):
"""This defines a sprite that is multiple images but is in a image
file that
has only it's images. But this sprite can move."""
def __init__(self, imageName, startx=0, starty=0,targetx=0,
targety=0, speed=2,
height=0, width=0):
MoveableSprite.__init__(self, imageName, startx, starty,
targetx, targety, speed)
AnimatedSheetSprite.__init__(self, imageName, startx, starty,
height, width)

def update(self):
MoveableSprite.update(self)
AnimatedSheetSprite.update(self)


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