[Tutor] A Simple Tkinter Control Program--Slight Problem

2009-03-05 Thread Wayne Watson


Here's what I think the author meant in discussing a control variable sample
program. 

from Tkinter import *

v=StringVar()

e = Entry(master, textvariable=v)
e.pack()
e.focus_set()

v.set("a default value")
s = v.get()

mainloop()

The problem is that Python objects with the msg:
   AttributeError: 'NoneType' object has no attribute 'tk'

What corrects this? The full sample program below it runs fine:
===
from Tkinter import *

master = Tk()

e = Entry(master)
e.pack()

e.focus_set()

def callback():
print e.get()

b = Button(master, text="get", width=10, command=callback)
b.pack()

mainloop()
--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Configuration File, Tkinter, IntVars--Manufacturing Variables

2009-03-05 Thread Wayne Watson




Yes, you have the idea. Here's about the simplest summary I wrote to
another person that I can muster. It may clarify matters. The anumber
I'm referring to is in the 80 line version I posted to start this
thread.

A Simple Explanation (I hope) 
Consider some of the new
code I put into the original 2000 line program written by someone else.


    def SetConfigDictionary():

    config_var_list = [
    {'name':'gray_scale', 'value':True, 'type': BOO},
    #  {'name':'color', 'value':None, 'type':
INT},  # gray_scale, ambiguity
    {'name':'post_event_stack', 'value':False, 'type': BOO},
    {'name':'post_event_format',  'value':'Tiff 2', 'type':
STR},
    {'name':'show_real_time', 'value':False, 'type': BOO},
    {'name':'hourly_rate', 'value':12, 'type': INT},
    {'name':'slowdown', 'value':1, 'type': INT},
    {'name':'start_time', 'value':'18:00:00', 'type': TIM},
    {'name':'stop_time',  'value':'10:00:00', 'type': TIM},
                blah, blah, ...
                ]
Like the self.anumber = 150 one will find
in the original program self.hourly_rate = 12, but it's now replaced by
running through that conf_var_list, and forming from the entry there
something like:  setattr(self, varName,
varValue), where varName is "hourly_rate" and varValue is 12. That is,
the program code no longer contains the explicity, hard coded,
self.hourly_rate = 12. Eventually, all this info is used to initialize
the big dialog in the image I sent (See a later response to Alan). It
all works just fine. However,
what has temporaily stumped me is how to retrieve any data the user
enters without use of the hard coded control variable references. That
activity takes place after (in 80 line version) dialog =
Enter_Data_Dialog( self.master,
sdict ).  Actually, I think I know, but haven't had the time to work
out the details. 
===End of Simple=

It looks like your sample code code has the right idea, but the hang up
with the original code is getting entered values back to the main
program and available globally through it. The trick in your code, I
think, is to get the data back the user entered, and set the main
program's global variables (to mimic the original program). I'm stuck
with the original concept of the main code setting self.hourly_rate to
the new value to, say, 15 in the Dialog setup that calls the Dialog
GUI. That is, when the Dialog returns, the code must set
self.hourly_rate to 15. I may not like that, but that's the way the
author designed it. I do not want to rewrite lots of code because of
it. So instead of the code after the return to the from Dialog being
hard coded, I need to construct executable statements from the list
above to do the job. 

Kent Johnson wrote:

...

  
I've stayed out of this because I don't really understand what you are
trying to do. But I *think* what you are looking for is a data-driven
GUI. Rather than hard-coding a bunch of variable names and types, you
want to build a gui based on a description of the data.

Here is a program that might give you some ideas about how to abstract
your problem. I don't expect it to exactly (or even closely) do what
you want but maybe it will point you in a useful direction. The heart
of it is the initial classes, which abstract the creation of a gui and
retrieval of a typed value, and the 'values' list in class App, which
is the definition of the values to display and their types. The gui
can be extended with additional entry lines simply by adding more
entries to the values list.

Kent

from Tkinter import *

# These classes encapsulate creating a gui element and retrieving a
typed value from it
# The constructors create the elements, the get() methods return values

class TextWidget(object):
def __init__(self, master, label, row):
Label(master, text=label).grid(row=row, sticky=W)
self.entry = Entry(master)
self.entry.grid(row=row, column=1)

class StringWidget(TextWidget):
def get(self):
return self.entry.get()

class IntWidget(TextWidget):
def get(self):
# This will blow up if the entry is not a valid integer
# Better would be to validate or constrain the input
value = self.entry.get()
return int(value) if value else 0

class BoolWidget(object):
def __init__(self, master, label, row):
self.var = IntVar()
cb = Checkbutton(master, text=label, variable=self.var)
cb.grid(row=row, columnspan=2, sticky=W)

def get(self):
return bool(self.var.get())

class App:
def __init__(self, master):
# List of parameter name, parameter description, widget type
values = [
('name', 'Your name:', StringWidget),
('age', 'Your age:', IntWidget),
('subscribe', 'Send emails', BoolWidget),
]


Re: [Tutor] Configuration File, Tkinter, IntVars--Manufacturing Variables

2009-03-05 Thread Kent Johnson
On Thu, Mar 5, 2009 at 11:36 AM, Wayne Watson
 wrote:

> It looks like your sample code code has the right idea, but the hang up with
> the original code is getting entered values back to the main program and
> available globally through it. The trick in your code, I think, is to get
> the data back the user entered, and set the main program's global variables
> (to mimic the original program). I'm stuck with the original concept of the
> main code setting self.hourly_rate to the new value to, say, 15 in the
> Dialog setup that calls the Dialog GUI. That is, when the Dialog returns,
> the code must set self.hourly_rate to 15. I may not like that, but that's
> the way the author designed it. I do not want to rewrite lots of code
> because of it. So instead of the code after the return to the from Dialog
> being hard coded, I need to construct executable statements from the list
> above to do the job.

This is not a big change from the code I wrote. Instead of printing
the values, return a dict mapping names to values:
  return dict((name, widget.get()) for name, widget in self.widgets.items())

Then in the caller, use setattr() to set the attributes on self:
  for name, value in returned_values.items():
setattr(self, name, value)

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A Simple Tkinter Control Program--Slight Problem

2009-03-05 Thread Alan Gauld


"Wayne Watson"  wrote

Here's what I think the author meant in discussing a control variable 
sample

program. 

from Tkinter import *

v=StringVar()

e = Entry(master, textvariable=v)

AG >> No master defined... you need

AG >> master = Tk()


e.pack()
e.focus_set()

v.set("a default value")
s = v.get()

mainloop()

AG>> and this should be master.mainloop()


Alan G 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Designing Tools/books

2009-03-05 Thread amit sethi
Although this may not be the most appropriate place to ask this but i
couldn't figure out where else i should be asking this question.I am getting
habitual of starting projects and getting results quickly thanks to python
.But I get stuck in documentation because i haven't asked the design
questions through and through.Although my projects work for college because
they are not large scale and do not require optimum quality control so this
problem only seems to come during documentation but i believe this will be a
pain when i do this professionally.So my question is please guide me to good
internet resources ,books and tools that talk about design especially with
python in mind. Also they follow Agile Software development model or any
other latest development cycle used in the industry .
-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A Simple Tkinter Control Program--Slight Problem

2009-03-05 Thread Wayne Watson
Title: Signature.html




master!   Good. Thanks.
 
master.mainloop()

Alan Gauld wrote:

"Wayne Watson"  wrote
  
  
Here's what I think the author meant in discussing a control variable
sample
  
program. 
  

  
from Tkinter import *
  
  
v=StringVar()
  
  
e = Entry(master, textvariable=v)
  
  
AG >> No master defined... you need
  
  
AG >> master = Tk()
  
  
  
e.pack()
  
e.focus_set()
  
  
v.set("a default value")
  
s = v.get()
  
  
mainloop()
  
  
AG>> and this should be master.mainloop()
  
  
  
Alan G 
  
___
  
Tutor maillist  -  Tutor@python.org
  
http://mail.python.org/mailman/listinfo/tutor
  
  


-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)


“In mathematics you don't understand things. 
 You just get used to them.” -- John Von Neumann
(P.S. The same is true in life.)




Web Page: 




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Designing Tools/books

2009-03-05 Thread Alan Gauld


"amit sethi"  wrote

problem only seems to come during documentation but i believe this 
will be a
pain when i do this professionally.So my question is please guide me 
to good
internet resources ,books and tools that talk about design 
especially with

python in mind.


I don't know of any that focus strictly on design with Python but the
principles of design are fairly laguage agnostic. Indeed a good test
of a design is whether it is easily translated into another language,
if not it is probably too low level!

Design breaks down to a few core principles
Try searching Wkipedia for terms like:
cohesion, coupling, modularity, encapsulation, interface
Follow the references accompanying those pages.

Some types of problem lend themselves to formal methods,
particularly state machines and logic dominated domains.

Some standard books that are worth reading are:

Design Patterns by the "Gang of Four"
OO Design and Analysis by Grady Booch
OO Design by Peter Coad - this one is greatlyy under rated IMHO

Many people like
Agile Sioftware Development by Robert Martin
but personally I didn't like it much.

Also worth considering is

Writing Effective Usecases by Cockburn
This is one I keep going back to and finding fresh insights each time.
In an Agile world high level use cases are a great way of grouping
development User Stories into deliverable and effective business
scenarios

Finally remember that design documents are not primarily written
for the developers, they should be aimed primarily at the testers,
trainers, project managers, operations and support teams.
They are the people who actually read and use design documents.
Developers create designs and maintainers read the code...


Also they follow Agile Software development model or any
other latest development cycle used in the industry .


Agile is very effective on small projects. But then again almost
anything is effective in small projects! As projects get bigger
Agile breaks down and becomes a hindrance on really big
projects. You can use Agile at the programmer level on a big
project but you will likely need a more architectural focussed
approach  to pull it all together. Design is largely independent
of the project management style used. The amount of
documentation produced and its format is almost entirely
driven by the project management style.
Design != Documentation

So a lot will depend on the kind of organisation you intend to
work for, the kind of projects they do and other such factors.

HTH,


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



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] array and ndarray

2009-03-05 Thread Mr Gerard Kelly
I am trying to convert something using the old Numeric module to the numpy 
module.

This is the code so far::

from __future__ import division

import pygame, time, random, pygame.sndarray
sample_rate = 44100

from numpy import *

def sine_array(hz,peak,n_samples=sample_rate):
  length=sample_rate/hz
  omega=pi*2/length
  xvalues=arange(length)*omega
  sinarray=(peak*sin(xvalues).astype(int16))
  sa=resize(sinarray,sample_rate)
  return sa

def play_for(sample_array, ms):
  pygame.mixer.pre_init(sample_rate, -16, 1)
  pygame.init()
  sound = pygame.sndarray.make_sound(sample_array)
  sound.play(-1)
  pygame.time.delay(ms)
  sound.stop()

def main():
  pygame.mixer.pre_init(sample_rate, -16, 1)
  pygame.init()
  play_for(sine_array(440,4096), 4000)

if __name__ == '__main__': main()



My error is the following:

Traceback (most recent call last):
  File "na.py", line 30, in 
if __name__ == '__main__': main()
  File "na.py", line 28, in main
play_for(sine_array(440,4096), 4000)
  File "na.py", line 20, in play_for
sound = pygame.sndarray.make_sound(sample_array)
  File "/usr/lib/python2.5/site-packages/pygame/sndarray.py", line 123, in 
make_sound
return numericsnd.make_sound (array)
TypeError: argument 1 must be array, not numpy.ndarray


How could I modify this to get an array instead of an ndarray?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Misunderstanding the Entry Widget

2009-03-05 Thread Wayne Watson
Title: Signature.html




Apparently Entry does not have a callback. It seems as though it
should. If one enters data into it and presses Return, then it would be
good to know what the data is, so it can be used elsewhere. However,
that's not the way it works. One seems to need a button or some other
widget to make it happen*. So what's the game with it?

* I have looked at 3 Entry examples on the web that appear to be well
written, and none of them really return anything, although they
supposedly should print the entry data once their button is pressed.
Well, OK, one does, but only after I had to remove the dash from the
name and replace it with an underscore. Whoops, that one doesn't really
work either. 
-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)


“In mathematics you don't understand things. 
 You just get used to them.” -- John Von Neumann
(P.S. The same is true in life.)




Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor