Re: [Tutor] What’s the differences between these two pieces of code ?

2012-07-13 Thread Chris Hare

On Jul 6, 2012, at 11:59 PM, redstone-cold wrote:

> What’s the differences between these two  pieces of code ?
> (1)
> for i in range(1, 7):
> print(2 * i, end='   ')
>  
>  
> (2)
> for i in range(1, 7):
> print(2 * i, end='   ')
> print()
>  
I think they are exactly the same, except the second example will print an 
extra "blank" line before exiting


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


Re: [Tutor] advice on global variables

2012-07-11 Thread Chris Hare

On Jul 11, 2012, at 8:05 AM, Walter Prins wrote:

> [snip]

> Your original example modified as demonstration:
> 
> a.py:
> 
> import shared
> import b
> 
> def func1():
>print "global var in func1 = %s" % shared.global_var
> 
> class intclass:
>def func2(self):
>print "global var in intclass = %s" % shared.global_var
> 
> print "global_var = %s" % shared.global_var
> func1()
> f = intclass()
> f.func2()
> g = b.extclass()
> g.func3()
> 
> b.py:
> 
> 
> import shared
> 
> class extclass:
>def func3(self):
>print "global var in extclass = %s" % shared.global_var
> 
> 
> shared.py:
> ===
> global_var = "global"
> 
> 
I like where this is going Walter.  I guess where I am confused is this:

the globals are not static - they are set once and then won't change during the 
lifetime of the user's session.   

So, after messing around with the ram DB idea, I realized I was back to the 
same problem.

Let's say I create a dictionary to store all of the "globals" and other data I 
want available everywhere and I put that in a class.  Every time I create an 
instance of the class to be able to access the data, a new instance of the 
dictionary is created that loads the same data in it.  Seems kinda inefficient 
for me, especially if the data changes in one instance - how do you keep the 
others all in sync, or does that just happen automatically?

I think there is something that I fundamentally not understanding and I don't 
know what it is.   Am I making this too complicated?  



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


Re: [Tutor] advice on global variables

2012-07-10 Thread Chris Hare

On Jul 10, 2012, at 6:24 PM, Alan Gauld wrote:

> On 11/07/12 00:16, Alan Gauld wrote:
> 
>>> One thought was a RAM based SQLite database, but that seems
>> > like a lot of work.  I dunno, maybe that is the option.
>> 
>> is definitely the best option where the "global" needs to be shared
>> across different programs as well as different modules in a single
> 
> I meant to add its also the right technique where the 'global' value has to 
> persist between different execution cycles of the program. (Or indeed any 
> kind of value needs to persist across execution cycles!)
> 

Thanks Alan -- I am thinking I am just gonna go with the RAM based SQLite 
database …. 
> -- 
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] advice on global variables

2012-07-10 Thread Chris Hare

I know they are bad.  That is why I would prefer not to use it, but I am not 
sure how else to handle this problem.

In this app, the user must log in.  Once authenticated, they have a userid 
stored in the SQLite database.  Before splitting my app into multiple files, I 
used a global variable.  I know its bad, but it worked.  Now that things are 
split apart, the classes which used it previously now don't see it anymore, 
even using the global keyword.  I think this is the expected behavior.  See here

file: a.py

import b
global_var = "global"

def func1():
global global_var
print "global var in func1 = %s" % global_var

class intclass:
def func2(self):
global global_var
print "global var in intclass = %s" % global_var

print "global_var = %s" % global_var
func1()
f = intclass()
f.func2()
g = b.extclass()
g.func3()

file: b.py

class extclass:
def func3(self):
global global_var
print "global var in extclass = %s" % global_var

When I run it, I get what I think the expected behavior, that the external 
class ext class won't be able to see the global_var

Big-Mac:t chare$ python a.py
global_var = global
global var in func1 = global
global var in intclass = global
Traceback (most recent call last):
  File "a.py", line 18, in 
g.func3()
  File "/Users/chare/Development/python/animaltrax/pkg/t/b.py", line 5, in func3
print "global var in extclass = %s" % global_var
NameError: global name 'global_var' is not defined

So - my question is this:  how do I solve the problem of multiple classes 
needing to get access to a value which needs to be preserved across the 
lifetime of the running application?

One thought was a RAM based SQLite database, but that seems like a lot of work. 
 I dunno, maybe that is the option.

suggestions, ideas, criticisms are all welcome.  Python code aside, I just 
don't know how to approach this problem in Python.

Thanks, as always for the feedback and guidance.

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


Re: [Tutor] str object is not callable

2012-07-10 Thread Chris Hare

Okay - I am officially embarrassed. 

As you might now, I am splitting this 10,000 line file apart, and that is 
posing certain challenges which I am fixing, and cleaning up stuff that was 
broken and visible only when doing this split.  

This is one of them.  

What I failed to remember -- and you guys are free to bash me for this  -- was 
so simple once I put a print in to see what the value was of the string 
entering the function.

I had (after modifications suggested):

def special_match(s, hunt=SPECIAL_CHARS.search):

BUT

This is defined in a Class - and I missed it.  Should have been

def special_match(self,s, hunt=SPECIAL_CHARS.search):

Sorry guys - but thanks for the excellent suggestions/advice.  I am sure not a 
python guru, python is my entry into OOP.  So, rest assured I come to the list 
after trying to figure it out, but lesson learned.  That is why it worked in 
one piece of code - it was standalone, and didn't work in the class where it 
has been moved to.  No excuse, but there it is nonetheless.


Chris



On Jul 10, 2012, at 10:33 AM, Devin Jeanpierre wrote:

> On Tue, Jul 10, 2012 at 10:56 AM, Chris Hare  wrote:
>> The input to the function in the larger program is the same as the first 
>> test in the small script that works -- "admin".
>> 
>> As a side note -- the rstrip call is also broken, although the string module 
>> is imported.  I just can't figure out why this code works in one context and 
>> not in another.
> 
> I suspect you defined "bool" somewhere to be a string. That, or else
> you passed in a string as the search argument. Unfortunately Python
> doesn't tell you which expression raised the exception, but certainly
> it's one of those two.
> 
> -- Devin

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


[Tutor] str object is not callable

2012-07-10 Thread Chris Hare

This piece of code works:

Big-Mac:Classes chare$ more tmp.py
import re

def special_match(string, search=re.compile(r'[^a-zA-Z0-9\.\ 
\-\#\$\*\@\!\%\^\&]').search):
#string = string.rstrip()
return not bool(search(string))

print special_match("admin")
print special_match("&!*")
print special_match("=")

Big-Mac:Classes chare$ python tmp.py
True
True
False
Big-Mac:Classes chare$ 

However, when I use the EXACT same code in the context of the larger code, I 
get the error

return not bool(search(strg))
TypeError: 'str' object is not callable

The input to the function in the larger program is the same as the first test 
in the small script that works -- "admin".  

As a side note -- the rstrip call is also broken, although the string module is 
imported.  I just can't figure out why this code works in one context and not 
in another.


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


Re: [Tutor] confusion about imports

2012-07-09 Thread Chris Hare
Good advice - thanks for that.  And I think you're right - I think what is 
happening is in fact a bunch of circular references.  As I resolve issues, I 
will be looking for those!  Appreciate all the advice!

On Jul 9, 2012, at 5:16 PM, Dave Angel wrote:

> On 07/09/2012 11:56 AM, Chris Hare wrote:
>> So, I have to admit, imports have me really confused.  I am trying to break 
>> apart a 10,000+ line single file into various files, one for each class, and 
>> one containing a whole bunch of functions which are used by a lot of 
>> classes.  Some of those functions use calls to methods in a Class.  Even 
>> though the Class has been imported, I get a nameError where trying to use 
>> the class.  I have read about Classes and packages and modules, but import 
>> just has me confused.
> Something I haven't seen explicitly mentioned in this thread is that
> when you make those modules to hold classes DO NOT make the module name
> the same as the class name.
> 
> If you have a class MyClass defined in your 10k file, and you want to
> move it to a separate file, and if you really wanted to dedicate the
> file to a single class, then the file might be called myclass.py and the
> references to it would like something like:
> 
> import myclass
> ...
> obj = myclass.MyClass(arg1, arg2)
> 
> or alternatively,
> 
> from myclass import MyClass
> ...
> obj = MyClass(arg1, arg2)
> 
> You wouldn't believe how much confusion people get into when they have
> module names that look like class names.
> 
> 
> Another thing is that you probably want several related classes and
> functions in each module.  This is not Java.  At that point, you might want
> 
> from  people  import MyFirstPeopleClass, MySecondPeopleClass, peoplefunction
> 
> 
> Finally, upon more careful reading of your original query, you may have
> circular dependencies happening here.  A function may use class methods,
> and class methods may use the function.  But if both are true, then put
> them in the same module.  Having one module import a second one which
> imports the first is an invitation to disaster.  And a special place in
> debugging hell is reserved for those that try to import the script that
> invokes it all.
> 
> -- 
> 
> DaveA
> 

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


Re: [Tutor] confusion about imports

2012-07-09 Thread Chris Hare

On Jul 9, 2012, at 12:42 PM, Walter Prins wrote:

> Hi Chris
> 
>> So, I have to admit, imports have me really confused.  I am trying to break 
>> apart a 10,000+ line single file into various files, one for each class, and 
>> one containing a whole bunch of functions which are used by a lot of 
>> classes.  Some of those functions use calls to methods in a Class.  Even 
>> though the Class has been imported, I get a nameError where trying to use 
>> the class.  I have read about Classes and packages and modules, but import 
>> just has me confused.
> 
> How did you import the class?  Or did you perhaps not import the Class
> itself, but rather the module containing the class?

I am doing import NAME where name is not only the name of the class, but also 
the name of the file containing the class.  The only reasons I am trying to 
break up the file is because it is getting to difficult to find stuff.  Some of 
the content hasn't change while other parts are still in flux a lot.  I figured 
that splitting it into the various files will make it easier to edit.

so I think I have figured out the problem based upon the resources you 
specified - the first one helped a bunch.

I was using 

import functions
import os
import db

when everything was all in one file, that worked just fine.  Now, with it all 
split up, once I changed

r = DbPath()

to

r = functions.DbPath()

things seems to work now.  I hope this is it!!!

Now, I have a bunch of smaller, more manageable files instead of trying to edit 
one ginormous one :-)

Thanks!

> 
> Read this article which explains a bit about Python namespaces:
> http://is.gd/e8PAZW  (but I note there's a bit of conflation of
> "class" and "class instance" going on at the end.)
> 
> Also read this page from the Python documentation, section "Python
> Scopes and Namespaces": http://docs.python.org/tutorial/classes.html
> 
> If a class is defined in a module (e.g. in the namespace of the
> module), and you "import module" the module into the your current
> namespace, then from within the current namespace you can access the
> class with "module.Class".  If however you import the class itself,
> e.g. "from module import Class", into your current namespace, then the
> Class itself is directly part of your local namespace and you must
> therefore access it unqualified as just "Class".

Thanks for the links -- yep - it helped, although I haven't solved my specific 
problem yet.
> 
> HTH,
> 
> Walter
> ___
> 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] confusion about imports

2012-07-09 Thread Chris Hare

So, I have to admit, imports have me really confused.  I am trying to break 
apart a 10,000+ line single file into various files, one for each class, and 
one containing a whole bunch of functions which are used by a lot of classes.  
Some of those functions use calls to methods in a Class.  Even though the Class 
has been imported, I get a nameError where trying to use the class.  I have 
read about Classes and packages and modules, but import just has me confused.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using dynamic import statements

2012-07-09 Thread Chris Hare

Thanks all for the ideas.  I wanted to have my own error messages printed for 
the user - something a little more meaningful than the standard error. 

Thanks for the advice - very helpful!

On Jul 9, 2012, at 6:12 AM, Alan Gauld wrote:

> On 09/07/12 10:19, Kwpolska wrote:
>> Why does this bloody ML want me to respond to the last person instead
>> of tutor@python.org?
> 
> Because that's how it, along with many other mailing lists, works.
> If it helps, think of it as you receiving a mail from the sender and CCd to 
> the list. Therefore hitting reply sends to the person who sent the mail and 
> ReplyAll goes to everyone. Seems logical to me! :-)
> It allows me to choose to reply to the OP only or to the group, I use both 
> depending on the nature of my reply. (About 80% of my replies go to the 
> everyone.) But some prefer it differently... :-)
> 
>>> Why not the more usual:
>>> 
>>> import sys, os, imp, stat,\
>>>re, webbrowser, Image, \
>>>StringIO, shutil, datetime
>> 
>> Why not the more standard:
>> import sys
>> import os
>> and so on?  http://www.python.org/dev/peps/pep-0008/#imports
> 
> Indeed but the OP seemed to want to remove duplicate coding so I assumed that 
> he included using multiple imports.
> 
> Personally I'd probably code the above as:
> 
> import sys, os, shutil, stat, datetime
> import re, StringIO
> import webbrowser,
> import Image,
> import imp
> 
> Which groups things into roughly related categories - system,
> strings, other...
> 
> OTOH I ignore large chunks of Pep 8 because I find its style harder to read 
> than the one I'm used to. But then, I'm not contributing to the standard 
> library etc... Style is largely a matter of taste.
> 
> -- 
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] using dynamic import statements

2012-07-08 Thread Chris Hare

Here is what I want to do:

I have a bunch of modules to import. instead of duplicating a lot of code for 
each import, I want to do something like this:

importList = [ "sys", "os", "imp", "stat", "re", "webbrowser", "Image",  
"StringIO", "shutil", "datetime" ]

for object in importList:
try:
if debug == "ON":
print "Importing module %s" % (object)
exec( "import  " + object)
except ImportError as error:
print "%s %s requires the Python %s library.  " % ( appName,

str(appVersion), object )
print "An error occurred when attempting to load the library.  
The error was '%s'." % ( error )
exit()

Everything "appears" to run okay, however, when the first piece of code that 
relies upon one of these imported modules is executed, I get an error:  

Traceback (most recent call last):
  File "a.py", line 122, in 
imp.load_module(object,fp,pathName,description)
  File "./Modules/functions.py", line 133, in 
def special_match(strg, search=re.compile(r'[^a-zA-Z0-9\.\ 
\-\#\$\*\@\!\%\^\&]').search):
NameError: name 're' is not defined

Is is possible to do what I am trying to do, or am I doing something wrong?

Thanks

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


Re: [Tutor] looking for some advice

2012-05-07 Thread Chris Hare
Thanks Peter - I will give it a look

On May 7, 2012, at 1:02 PM, Peter Otten wrote:

> Chris Hare wrote:
> 
>> Hello Everyone:
>> 
>> Here is what I am trying to do:
>> 
>> I have a window which has a row of buttons on it.   Below the buttons is a
>> label frame.  Depending upon which button they push, I want to change the
>> widgets in the label frame.  I can add widgets now with no problem.
>> 
>> Basically, I am trying to imitate a notebook like in Tkinter.ttk, without
>> having to re-write a major chunk of the app to add such a widget.  I know
>> Tix has a notebook widget, but that would require people (read end users)
>> to download and compile the Tix components.
>> 
>> My questions are:
>> 
>> 1.  how do I remove all of the widgets from the label frame to add the new
>> ones?
>> 2.  Am I better off just creating different label frames for each group of
>> widgets and then just using grid_forget and grid_remember to hide or show
>> them as I need them?
>> 3.  How else would you approach this problem?
>> 
>> Yes - I guess I am looking for some design advice.  This is creeping a
>> little since my customer (my wife who is a horse breeder) would like to
>> have some things done a little differently in her app.
>> 
>> Thanks for your suggestions!
> 
> There is a tabbedpages.TabbedPageSet widget in idlelib. At first glance I 
> don't see any dependencies, so maybe you can use that?
> 
> ___
> 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] looking for some advice

2012-05-07 Thread Chris Hare
Hello Everyone:

Here is what I am trying to do:

I have a window which has a row of buttons on it.   Below the buttons is a 
label frame.  Depending upon which button they push, I want to change the 
widgets in the label frame.  I can add widgets now with no problem.   

Basically, I am trying to imitate a notebook like in Tkinter.ttk, without 
having to re-write a major chunk of the app to add such a widget.  I know Tix 
has a notebook widget, but that would require people (read end users) to 
download and compile the Tix components.  

My questions are:

1.  how do I remove all of the widgets from the label frame to add the new 
ones?  
2.  Am I better off just creating different label frames for each group of 
widgets and then just using grid_forget and grid_remember to hide or show them 
as I need them?
3.  How else would you approach this problem?

Yes - I guess I am looking for some design advice.  This is creeping a little 
since my customer (my wife who is a horse breeder) would like to have some 
things done a little differently in her app.

Thanks for your suggestions!

Chris

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


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


Re: [Tutor] events and popup menus

2012-05-02 Thread Chris Hare
Using the event coordinates to figure out which widget was clicked sounds like 
a good idea.  So, since the user might have moved the window on the screen, how 
do I figure out the widget's coordinates in order to figure out which of the 
four widgets the mouse was over when the user clicked a button?  this is the 
first time I am doing this so I am not even sure what I need to search for 
online.  
Thanks



On May 1, 2012, at 7:31 PM, Alan Gauld wrote:

> On 01/05/12 21:59, Chris Hare wrote:
>> ... what I can't figure out is how to detect in the popup menu code
> > which image fired the event
> 
> 
>>  def do_popup(self,event):
> 
> 
> The event argument has various attributes. For a mouse click it should 
> include the screen coordinates. You can use those to determine which widget 
> was being clicked.
> 
> HTH
> 
> -- 
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] events and popup menus

2012-05-01 Thread Chris Hare

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)

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!

Chris


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


[Tutor] putting an image into a canvas object

2012-04-28 Thread Chris Hare

What I am trying to do is put an image on a canvas object, which I think is 
allowed.

self.picture1 = Canvas(self.pictureFrame,width=150,height=150)

self.imageBuffer = StringIO.StringIO()

image = Image.open(filename)
image = image.resize((150,150),Image.ANTIALIAS)
image.save(self.imageBuffer, format= 'PNG')

self.imageBuffer.seek(0)
image = Image.open(self.imageBuffer)

photo = PhotoImage(image)
self.picture1.create_image(0, 0, image = photo )

However, this code results in the following exception:"
Exception in Tkinter callback
Traceback (most recent call last):
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
 line 1410, in __call__
return self.func(*args)
  File "z.py", line 803, in changePicture1
self.picture1.create_image(0, 0, image = photo )
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
 line 2198, in create_image
return self._create('image', args, kw)
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
 line 2189, in _create
*(args + self._options(cnf, kw
TypeError: __str__ returned non-string (type instance)

What did I do wrong?

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


Re: [Tutor] PIL and converting an image to and from a string value

2012-04-28 Thread Chris Hare
What I did:

self.imageBuffer = StringIO.StringIO()
image = Image.open(filename)
image = image.resize((150,150),Image.ANTIALIAS)
image.save(self.imageBuffer, format= 'PNG')
self.imageBuffer.seek(0)
image = Image.open(self.imageBuffer)

So, that is how I got around the problem


On Apr 27, 2012, at 1:22 PM, Russell Smith wrote:

> What did you do?
> 
> On Friday, April 27, 2012, Chris Hare wrote:
> 
> I got it figured out.
> 
> On Apr 27, 2012, at 12:21 AM, Chris Hare wrote:
> 
> >
> > Here is what I am trying to:
> >
> > the application user chooses an image file.  I want to store the image data 
> > in a field in a sqlite database.  My understanding from the reading I have 
> > done is that I have to convert the image data into a string , which I can 
> > then store in the database.  Additionally, I need to be able to take the 
> > stored image data and convert it back to an image  so I can display it in a 
> > canvas widget.  I am using the python imaging library
> >
> > I think I have the following parts correct:
> >
> > image = Image.open("cover.jpg")
> > image = image.resize((150,150),Image.ANTIALIAS)
> > png = image.tostring("PNG")
> >
> > I am taking the image data from the file, resizing the image, and then 
> > using the PNG coder to force it into a PNG format image and converting it 
> > to a string.
> >
> > I am stuck however, in figuring out how to take the string data and 
> > converting it back to an image that I can put into the canvas widget.  
> > Either I am not finding a good explanation of how to do this or I am just 
> > not understanding what I am finding:-)
> >
> > Any ideas are appreciated!
> >
> > Thanks,
> > Chris
> >
> > ___
> > 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] PIL and converting an image to and from a string value

2012-04-27 Thread Chris Hare

I got it figured out.  

On Apr 27, 2012, at 12:21 AM, Chris Hare wrote:

> 
> Here is what I am trying to:
> 
> the application user chooses an image file.  I want to store the image data 
> in a field in a sqlite database.  My understanding from the reading I have 
> done is that I have to convert the image data into a string , which I can 
> then store in the database.  Additionally, I need to be able to take the 
> stored image data and convert it back to an image  so I can display it in a 
> canvas widget.  I am using the python imaging library
> 
> I think I have the following parts correct:
> 
> image = Image.open("cover.jpg")
> image = image.resize((150,150),Image.ANTIALIAS)
> png = image.tostring("PNG")
> 
> I am taking the image data from the file, resizing the image, and then using 
> the PNG coder to force it into a PNG format image and converting it to a 
> string.
> 
> I am stuck however, in figuring out how to take the string data and 
> converting it back to an image that I can put into the canvas widget.  Either 
> I am not finding a good explanation of how to do this or I am just not 
> understanding what I am finding:-)
> 
> Any ideas are appreciated!
> 
> Thanks,
> Chris
> 
> ___
> 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] PIL and converting an image to and from a string value

2012-04-26 Thread Chris Hare

Here is what I am trying to:

the application user chooses an image file.  I want to store the image data in 
a field in a sqlite database.  My understanding from the reading I have done is 
that I have to convert the image data into a string , which I can then store in 
the database.  Additionally, I need to be able to take the stored image data 
and convert it back to an image  so I can display it in a canvas widget.  I am 
using the python imaging library

I think I have the following parts correct:

image = Image.open("cover.jpg")
image = image.resize((150,150),Image.ANTIALIAS)
png = image.tostring("PNG")

I am taking the image data from the file, resizing the image, and then using 
the PNG coder to force it into a PNG format image and converting it to a string.

I am stuck however, in figuring out how to take the string data and converting 
it back to an image that I can put into the canvas widget.  Either I am not 
finding a good explanation of how to do this or I am just not understanding 
what I am finding:-)

Any ideas are appreciated!

Thanks,
Chris

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


Re: [Tutor] executing dynamic code with exec?

2011-12-02 Thread Chris Hare
Thanks Steve for your help (and the humor).  I can see that it was a bad idea 
with your explanation.  (I just didn't want to type all that extra code :-))

I am going to re-write it using your dict approach - that looks a lot cleaner

Thanks!

Chris Hare
ch...@labr.net
http://www.labr.net

On Dec 1, 2011, at 11:25 PM, Steven D'Aprano wrote:

> Chris Hare wrote:
> 
>> What I am trying to do is create a set of variables based upon the table
>> names in the table variables.  I have similar code which dynamically
>> creates check buttons and the associated grid.  But I suspect those won't
>> work either.
>> What have I got wrong?
> 
> Everything! 
> 
> Seriously though, your basic approach is the wrong approach. Don't try to 
> create dynamic variables like that. Suppose you succeed:
> 
> varName = 'x'  # read from a file, or something
> exec('%s = 1' % varName)  # creates the variable x
> 
> Great. Now you have a variable x. Later on, how do you use it?
> 
> # much later on in your code...
> y = x + 1
> 
> But that won't work, because you don't know that it's called x! If you knew 
> it was called x, you would have just written x = 1 early and not needed exec.
> 
> Working with dynamic variable names is a pain and a nightmare. Don't do it. 
> Even if you succeed, you are making a rod for your own back: maintaining such 
> code is horrible.
> 
> The right way to do this is almost always to use a data structure that maps 
> names to values, in other words, a dict.
> 
> varName = 'x'  # read from a file, or something
> data = {varName: 1}
> # ...
> # much later
> y = data[varName] + 1
> 
> 
> In this case, something like:
> 
> 
> names = ["Farm", "Animals", "AnimalTypes", "Users", "Roles",
>"Capabilities", "Pedigrees", "ChipMaker", "Owner", "Providers",
>"RegistryL"
>]
> self.cbReadTable = {}
> for name in names:
>self.cbReadTable[name] = IntVar()
> 
> 
> And that's it. Instead of retrieving instance.cbFarmRead, use 
> instance.cbReadTable['Farm'].
> 
> If you absolutely must use instance attributes, perhaps because you think 
> you're writing Javascript , then:
> 
> for name in names:
>name = 'cb' + name 'Read'
>setattr(self, name, IntVar())
> 
> 
> And best of all, you avoid the code injection security vulnerability where 
> somebody manages to fool your code into using a list of table names like:
> 
> names = ["Farm", "Animals", "AnimalTypes", "Users", "Roles",
>"Capabilities", "Pedigrees",
>"ChipMaker=1;import os;os.system('echo you are pwned rm-rf haha');",
>"Owner", "Providers", "RegistryL"
>]
> 
> 
> Hope your backups are really good.
> 
> http://xkcd.com/327/
> 
> 
> 
> -- 
> Steven
> 
> ___
> 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] executing dynamic code with exec?

2011-12-01 Thread Chris Hare

I have this code chunk:

tables = ["Farm", "Animals", 
"AnimalTypes","Users","Roles","Capabilities","Pedigrees","ChipMaker","Owner","Providers","RegistryL"]
for x in tables:
cmd = "self.cb" + x + "Read = IntVar()"
exec cmd in locals(), globals()

When the code is executed, I get the following error:

  File "z.py", line 4398
exec cmd in locals(), globals()
SyntaxError: function 'showRoles' uses import * and bare exec, which are 
illegal because it contains a nested function with free variables

What I am trying to do is create a set of variables based upon the table names 
in the table variables.  I have similar code which dynamically creates check 
buttons and the associated grid.  But I suspect those won't work either.

What have I got wrong?

Thanks!

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


[Tutor] advice

2011-11-28 Thread Chris Hare

I have been searching around a bit, but not really finding anything which is 
meaningful.

I want to embed a set of help files in my application, and display them from 
within the application.  I could use plain text files, which would be easy to 
suck into a Text field, but it would be nice to include rich text features and 
maybe even graphics.

Is there some way of displaying rich text files, such as RTF or HTML from 
within a python Tkinter widget?

Thanks

Chris


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


[Tutor] Tkinter grid manager question

2011-11-26 Thread Chris Hare

Hi everyone.

I am attempting to create a form for the user to complete.  I have the basic 
layout and supporting code working, but I am having a problem with getting 
things to display in the grid the way I want them.

self.label1 = Label(self.frame, text = "Double click on 
a name from the list in the right.  Make your changes in the field below and 
press the Save button.", fg="Blue", bg=backColor)
self.label2 = Label(self.frame, text = "Current Animal 
Types", fg="Blue", bg=backColor)
…...
self.label1.grid(row=1, column=0,columnspan=3, sticky=W)
self.label2.grid(row=2, column=0,columnspan=1, sticky=W)
self.list.grid(row=2, column=3, columnspan=2, sticky=W)
self.label3.grid(row=3, column=0,columnspan=1, sticky=W)

My problem is that the cell which contains the text for label2 is the same size 
as the text in label1.  What I am trying to accomplish is to have the text in 
label1 span the entire window, instead of having a huge cell with all of the 
label1 text in it.

Ideas?

Thanks!


Chris

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


Re: [Tutor] binding problem

2011-11-03 Thread Chris Hare
That helps Wayne - and was what I was referring to when I posted that I thought 
I had figured it out.  Thanks for your help.


Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 3, 2011, at 10:08 AM, Wayne Werner wrote:

> On Thu, Nov 3, 2011 at 9:41 AM, Chris Hare  wrote:
> Thanks Peter.  Actually, I have read a bunch of stuff and looked at example 
> code.  The problem in this case is I am using a defined method - focus_set(), 
> which is part of Tkinter and isn't part of my code.  since I am using it in 
> the manner in which I have seen other examples, I am confused about why it is 
> complaining about 2 arguments, when I am not passing any.
> 
> Although, I think I know what the problem is now.
> 
> The problem is that when you use .bind() Tkinter will pass an event into your 
> function. This event contains useful information such as the x,y position of 
> your mouse, the key that fired the event, and a few other items. Of course, 
> since focus_set() belongs to a class it will always pass 'self' as the first 
> parameter.
> 
> So when you bind widget.focus_set, when the event loop handles say, 
> , it finds the function bound (focus_set), and Python passes self 
> and Tkinter passes the event - two parameters, even though you're passing 
> none.
> 
> As has been touched on, the standard thing to do when you are binding an 
> event but you don't actually care about the event (like focus_set()), is to 
> use the lambda, which allows you to ignore the event argument:
> 
> self.list.bind("", lambda _: self.login_userid.focus_set())
> 
> It's convention to use the _ variable name to tell other programmers that you 
> don't care about that value, and you're intentionally ignoring whatever gets 
> passed to that function.
> 
> HTH,
> Wayne

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


Re: [Tutor] binding problem

2011-11-03 Thread Chris Hare
Thanks Peter.  Actually, I have read a bunch of stuff and looked at example 
code.  The problem in this case is I am using a defined method - focus_set(), 
which is part of Tkinter and isn't part of my code.  since I am using it in the 
manner in which I have seen other examples, I am confused about why it is 
complaining about 2 arguments, when I am not passing any.

Although, I think I know what the problem is now.

Thanks for your help anyway.  

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 3, 2011, at 9:08 AM, Peter Otten wrote:

> Chris Hare wrote:
> 
>> Thanks for the advice.  When I do that, I get this error
>> 
>> Exception in Tkinter callback
>> Traceback (most recent call last):
>>  File
>> 
> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-
> tk/Tkinter.py",
>>  line 1410, in __call__
>>return self.func(*args)
>> TypeError: focus_set() takes exactly 1 argument (2 given)
>> 
>> In situations like this where the function isn't one you wrote, how to you
>> debug these?
> 
> In this case there is not much to debug, the error message says it all: you 
> have an extra argument that the focus_set() method doesn't accept. The 
> simplest way to drop that argument is to wrap the method call:
> 
> def focus_set(event): # only one arg because it's a function, not a method
>self.login_userid.focus_set()
> 
> self.list.bind("", focus_set)
> 
> This is sometimes written with a lambda function
> 
> self.list.bind("", lambda event: self.login_userid.focus_set())
> 
> If you wanted to learn more about that extra argument you could temporarily 
> replace the callback with something that can give you the necessary 
> information, e. g.
> 
> def inspect_args(*args):
>for arg in args:
> print arg
> print vars(arg)
> 
> self.list.bind("", inspect_args)
> 
> As a last resort and an idea almost too revolutionary to mention in public, 
> you could read some tutorial or documentation...
> 
> ___
> 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] binding problem

2011-11-03 Thread Chris Hare
Thanks for the advice.  When I do that, I get this error

Exception in Tkinter callback
Traceback (most recent call last):
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
 line 1410, in __call__
return self.func(*args)
TypeError: focus_set() takes exactly 1 argument (2 given)

In situations like this where the function isn't one you wrote, how to you 
debug these?

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 3, 2011, at 3:23 AM, Alan Gauld wrote:

> On 03/11/11 04:46, Chris Hare wrote:
>> 
>> I have a Listbox defined
>> 
>> self.list = Listbox(self.frame)
>> 
>> What I want to do is when the user either single clicks, double clicks, 
>> presses tab or return, move the focus to the specified field
>> 
>> self.list.bind("", self.login_userid.focus_set())
> 
> Don't call the function, just pass its name:
> 
> self.list.bind("", self.login_userid.focus_set)
> 
> HTH,
> 
> -- 
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] frame destroy problem

2011-11-03 Thread Chris Hare

Dang it - sure is a typo!
Thanks!

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 3, 2011, at 12:58 AM, Dipo Elegbede wrote:

> There is nothing called franeButton it should be frameButton. I guess its a 
> typo.
> 
> On 3 Nov 2011 05:52, "Chris Hare"  wrote:
> 
> 
> I have the following code:
> 
>def listUsers(self):
>self.frameBottom = Frame(self.base, bd=0, bg=backColor)
>self.frameBottom.grid(row=1, column=0,sticky=N+E+S+W)
>self.text = Text(self.frameBottom)
>self.text.grid(row=1, column=6, columnspan=5, sticky=E)
>self.text.insert(END, security.listUsers())
>self.btnClose = Button(self.frameBottom, text="Close", 
> command=self.closeFrameBottom,highlightbackground=backColor)
>self.btnClose.grid(row=2, column=4)
> 
>def closeFrameBottom(self):
>self.franeBottom.destroy()
> 
> When the listUsers method is called, everything is displayed correctly.  
> However, when the  btnClose is pressed, I get an error
> 
> Exception in Tkinter callback
> Traceback (most recent call last):
>  File 
> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
>  line 1410, in __call__
>return self.func(*args)
>  File "z.py", line 454, in closeFrameBottom
>self.franeBottom.destroy()
> AttributeError: Display instance has no attribute 'franeBottom'
> 
> What have I got wrong? the objective is to use the bottom part opt the window 
> over and over again.
> 
> Chris Hare
> ch...@labr.net
> http://www.labr.net
> 
> ___
> 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


[Tutor] frame destroy problem

2011-11-02 Thread Chris Hare

I have the following code:

def listUsers(self):
self.frameBottom = Frame(self.base, bd=0, bg=backColor)
self.frameBottom.grid(row=1, column=0,sticky=N+E+S+W)
self.text = Text(self.frameBottom)
self.text.grid(row=1, column=6, columnspan=5, sticky=E)
self.text.insert(END, security.listUsers())
self.btnClose = Button(self.frameBottom, text="Close", 
command=self.closeFrameBottom,highlightbackground=backColor)
self.btnClose.grid(row=2, column=4)

def closeFrameBottom(self):
self.franeBottom.destroy()

When the listUsers method is called, everything is displayed correctly.  
However, when the  btnClose is pressed, I get an error

Exception in Tkinter callback
Traceback (most recent call last):
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
 line 1410, in __call__
return self.func(*args)
  File "z.py", line 454, in closeFrameBottom
self.franeBottom.destroy()
AttributeError: Display instance has no attribute 'franeBottom'

What have I got wrong? the objective is to use the bottom part opt the window 
over and over again.

Chris Hare
ch...@labr.net
http://www.labr.net

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


[Tutor] binding problem

2011-11-02 Thread Chris Hare

I have a Listbox defined

self.list = Listbox(self.frame)

What I want to do is when the user either single clicks, double clicks, presses 
tab or return, move the focus to the specified field

self.list.bind("", self.login_userid.focus_set())
self.list.bind("", 
self.login_userid.focus_set())
self.list.bind("", self.login_userid.focus_set())
self.list.bind("", self.login_userid.focus_set())

If I can get this working, I should be able to get the other bindings to work.
Chris Hare
ch...@labr.net
http://www.labr.net

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


Re: [Tutor] login window using Tk

2011-11-02 Thread Chris Hare

Just thought I would drop y'all a note and say thank you for your help on this. 
 I have the login code working.

I learned a bunch from you guys.

Thanks!

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 2, 2011, at 5:02 AM, Alan Gauld wrote:

> On 02/11/11 05:05, Chris Hare wrote:
> 
>> def verifyLogin(self):
>> farmid = list.get(ACTIVE)
>> userid = login_userid.get()
>> login_passwd = login_passwd.get()
>> 
>> gets called, but I get the error
>> 
>> Exception in Tkinter callback
>> farmid = list.get(ACTIVE)
>> AttributeError: type object 'list' has no attribute 'get'
>> 
>> When the frame controls were added, list is defined as
>> 
>> list = Listbox(frame)
> 
> names defined in functions are local to that function. They can't be seen 
> outside of the function. So...
> To be able to access the controls you need to add themas instance attributes 
> to your class. so you should use
> 
> self.list = Listbox(
> 
> and
> 
> farmid = self.list.get(
> 
> What you are seeing is Python looking for something called list in your 
> methods namespace and not finding it. It can't see anything global either so 
> it picks up the built-in list() type.
> 
> HTH
> 
> -- 
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using separate py files for classes

2011-11-02 Thread Chris Hare

A…. thanks Hugo!!

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 2, 2011, at 2:14 AM, Hugo Arts wrote:

> On Wed, Nov 2, 2011 at 7:29 AM, Chris Hare  wrote:
>> 
>> I would like to put each of my classes in separate files to make it easier
>> to edit them and keep the various files as small as possible for editing
>> purposes.
>> I have come across a couple of problems:
>> 1.  I have to use import statements like "from file import class" instead of
>> "import file"
>> 2.  I have to include all of the import statements for things not found in
>> the class file
>> Is this normal or am I doing something wrong?
>> Thanks!
>> Chris Hare
> 
> That's exactly how it's supposed to work, actually. One thing though,
> you can use "import file" rather than "from file import class." You
> then acces the class like "a = file.class()"
> 
> Hugo

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


[Tutor] using separate py files for classes

2011-11-02 Thread Chris Hare

I would like to put each of my classes in separate files to make it easier to 
edit them and keep the various files as small as possible for editing purposes. 
 

I have come across a couple of problems:

1.  I have to use import statements like "from file import class" instead of 
"import file"
2.  I have to include all of the import statements for things not found in the 
class file

Is this normal or am I doing something wrong?

Thanks!

Chris Hare
ch...@labr.net
http://www.labr.net

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Chris Hare

Thanks everyone for all of the help.  I almost have this working.

Everything is written in a class.  I think I have that right, but that remains 
to be seen. :-)

I can create the login window and get all of the controls on it.  My function 
gets called to validate the information in the fields when the user presses the 
button.  

the function called however, can't seem to be able to get the items from the 
fields.  I get the error like list.get(ACTIVE) doesn't have a function for get. 
 (list was defined as a listbox.)  the same is true for the other form fields. 
I opted to use the root window and implement a frame in it for the login.  Once 
the login data has been validated, i can destroy the frame and reuse the 
window.  This may or may not work ;-)  I am a python newbie, biting off a big 
complicated chunk 

The class is defined:
class Login:
def __init__(self, parent):
self.window = parent

When I show the class instance, everything is displayed.  My verification code,

def verifyLogin(self):
farmid = list.get(ACTIVE)
userid = login_userid.get()
login_passwd = login_passwd.get()

gets called, but I get the error

Exception in Tkinter callback
Traceback (most recent call last):
  File 
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
 line 1410, in __call__
return self.func(*args)
  File "z.py", line 229, in verifyLogin
farmid = list.get(ACTIVE)
AttributeError: type object 'list' has no attribute 'get'

When the frame controls were added, list is defined as

list = Listbox(frame)

What have I got messed up?  I have poked around the net but I can't find 
anything meaningful to me.

Thanks again


Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 1, 2011, at 7:50 PM, Alan Gauld wrote:

> On 01/11/11 21:28, Chris Hare wrote:
>> 
>> Good feedback Alan, thanks.
>> 
>> I wasn't using the root window to hold the login form, although I
>> suppose I could. I guess where I am stuck is the login to control
>> displaying the login window, and hiding it to display the actual
>> application window once the user has authenticated.
> 
> Thats what your command function does. So when the button is pressed your 
> event handler authenticates the user details, if valid it closes the Login 
> and shows the main window(which could be root...)
> In pseudocode:
> 
> 
> def doLogin(self):
>userid = idField.get()
>passwd = pwField.get()
>if self.validateUser(userid,passwd):
>root.show()
>self.window.hide()
>else:
>self.beep()   # or whatever warning message you want
>self.logError("User authentication failed for " + userid)
>self.idField.clear()
>self.pwField.clear()
> 
> Then in creating the button you pass that as the command handler:
> 
> btnLogin = Button(self.window, text="Login", command=doLogin)
> 
> Now, when the user hits the button the doLogin function will be called.
> If the login is ok we show the main window and hide the login dialog.
> If the entry is invalid we beep, clear the fields for a retry and log an 
> error. We could also add a count so after, say, three attempts we close the 
> app.
> 
> HTH
> -- 
> 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] login window using Tk

2011-11-01 Thread Chris Hare

Good feedback Alan, thanks.

I wasn't using the root window to hold the login form, although I suppose I 
could.  I guess where I am stuck is the login to control displaying the login 
window, and hiding it to display the actual application window once the user 
has authenticated.  

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 1, 2011, at 3:49 PM, Alan Gauld wrote:

> 

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Chris Hare
Okay - that makes sense.  The login window uses the show="*" for the password 
field and is authenticated against a database where the passwords are 
encrypted.  I have this working in a text only environment, just struggling to 
get it right for the GUI

Thanks

Chris Hare
ch...@labr.net
http://www.labr.net

On Nov 1, 2011, at 1:02 PM, Alan Gauld wrote:

> 

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


Re: [Tutor] login window using Tk

2011-11-01 Thread Chris Hare
Here is a code snippet I have pulled out of the project.  It is as bare bones 
as I can make it to get the point across.

the problems I am having:

1.  I would really like the window to be centered in the user's screen, but 
setting the geometry doesn't place it there.  (that isn't included here)
2.  When I click the Login button, nothing happens.  I know I am missing 
something but it just isn't obvious what it is.
3.  Finally, I would like to be able to hide the root window until the 
authentication is performed, but root.hide() gets me a getattr error.  
root.withdraw() works, but I can't get the root window back 

Thanks for your help.  

import sys
from Tkinter import *
import tkMessageBox
import tkFont

class Login:
def __init__(self,parent):
self.window = parent

def show(instance): 
window = Toplevel()
frame = Frame(window,bg=backColor)
menubar = Menu(window)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command="sys.exit()")
menubar.add_cascade(label="File", menu=filemenu)
window.config(menu=menubar)
programName = Label(frame, text = "test")
list = Listbox(frame)
list.insert( END,"test")
label1 = Label(frame, text = "Organization", fg="Blue", 
bg=backColor)
label2 = Label(frame, text = "Username", fg="Blue", 
bg=backColor)
label3 = Label(frame, text = "Password", fg="Blue", 
bg=backColor)
login_userid = Entry(frame,bg=outFocusColor)
login_passwd = Entry(frame,bg=outFocusColor,show="*")
login_userid.bind("", login_passwd.focus_set())
btnLogin = Button(frame, text="Login", command="print button 
pressed",highlightbackground=backColor)

frame.title = "Login to application" 
list.focus_set()
frame.grid()
programName.grid(row=0, column=0,columnspan=5,sticky=W)
label1.grid(row=1, column=0,columnspan=3, sticky=W)
list.grid(row=1, column=6, columnspan=5, sticky=W)
label2.grid(row=2, column=0,columnspan=3, sticky=W)
login_userid.grid(row=2, column=6, columnspan=5,sticky=W)
label3.grid(row=3, column=0,columnspan=3, sticky=W)
login_passwd.grid(row=3, column=6, columnspan=5,sticky=W)
btnLogin.grid(row=4, column=4, sticky=W)

if __name__ == "__main__":
backColor = "Gray"
entryColor = "Cyan"
okColor = "Green"
warnColor = "Red"
inFocusColor = "Cyan"
outFocusColor = "White"

root = Tk()
root.withdraw()
l = Login(root)
l.show()

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


[Tutor] login window using Tk

2011-11-01 Thread Chris Hare

I am working on a python Tk program which involves a login window and I am 
looking for some advice.

Currently the code I have creates a window (Toplevel) where the login controls 
are and I am running that using a main loop for the window.  The root window is 
hidden.  The objective is that when the user ha successfully authenticated, the 
login window is closed or the main loop os exited and then the root window is 
shown and the main loop started for the actual application.

Questions:
1.  Is this the best way of doing this or is there a better way?
2.  How do I exit the main loop when the user has authenticated?

Thanks

Chris Hare
ch...@labr.net
http://www.labr.net



Chris Hare
ch...@labr.net
http://www.labr.net

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