Python-list Digest, Vol 81, Issue 63

madhuri vio madhuri.vio at gmail.com
Wed Jun 9 02:57:18 EDT 2010


import tkinter

root = tkinter.Tk() #initialize tkinter and get a top level instance
root.title("madhuri is a python")
canvas = tkinter.Canvas(root) #creating the canvas under the root
canvas.pack() #to call the packer geometry
canvas.create_rectangle(20,10,120,80,fill=colors[0])
root.close()
tk.destroy()


this is the program i have written and i am unable to execute it as i
get an attribute error like this...

$ python tkinter.py
Traceback (most recent call last):
  File "tkinter.py", line 4, in <module>
    import tkinter
  File "/home/manoj/tkinter.py", line 6, in <module>
    root = tkinter.tk() #initialize tkinter and get a top level instance
AttributeError: 'module' object has no attribute 'tk'

where is the mistake and what do i do ???its a ll urgent

On Tue, Jun 8, 2010 at 3:30 PM, <python-list-request at python.org> wrote:

> Send Python-list mailing list submissions to
>        python-list at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>        http://mail.python.org/mailman/listinfo/python-list
> or, via email, send a message with subject or body 'help' to
>        python-list-request at python.org
>
> You can reach the person managing the list at
>        python-list-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Python-list digest..."
>
> Today's Topics:
>
>   1. Re: UnboundLocalError: local variable referenced before
>      assignment (Chris Rebert)
>   2. Re: Which objects are expanded by double-star ** operator? (Bryan)
>   3. Re: Which objects are expanded by double-star ** operator?
>      (kkumer)
>   4. Re: UnboundLocalError: local variable referenced before
>      assignment (ch1zra)
>   5. Re: Plotting in batch with no display (Giacomo Boffi)
>   6. Re: Plotting in batch with no display (Giacomo Boffi)
>   7. Re: Which objects are expanded by double-star ** operator?
>      (kkumer)
>   8. Re: Reading file bit by bit (Martin)
>
>
> ---------- Forwarded message ----------
> From: Chris Rebert <clp2 at rebertia.com>
> To: ch1zra <ch1zra at gmail.com>
> Date: Tue, 8 Jun 2010 02:25:02 -0700
> Subject: Re: UnboundLocalError: local variable referenced before assignment
> On Tue, Jun 8, 2010 at 2:00 AM, ch1zra <ch1zra at gmail.com> wrote:
> > On Jun 8, 10:29 am, Richard Thomas <chards... at gmail.com> wrote:
> >> On Jun 8, 9:03 am, ch1zra <ch1... at gmail.com> wrote:
> >> > I have following code :
> >>
> >> > import os, time, re, pyodbc, Image, sys
> >> > from datetime import datetime, date, time
> >> > from reportlab.lib.pagesizes import A4
> >> > from reportlab.lib.units import cm
> >> > from reportlab.pdfgen import canvas
> >> > from reportlab.pdfbase import pdfmetrics
> >> > from reportlab.pdfbase.ttfonts import TTFont
> >> > import mkTable
> >>
> >> > mkTable.mkTable()
> >>
> >> > and then file mkTable.py located in same directory has :
> >>
> >> > def mkTable():
> >> >     global canvas
> >> >     canvas = canvas.Canvas(fname, pagesize=A4)
> >> >     ... and so on
> >>
> >> > this gives me following traceback:
> >>
> >> > Traceback (most recent call last):
> >> >   File "C:\py\pdf_test.py", line 36, in <module>
> >> >     mkTable.mkTable()
> >> >   File "C:\py\mkTable.py", line 38, in mkTable
> >> >     canvas = canvas.Canvas("K_lista.pdf", pagesize=A4)
> >> > UnboundLocalError: local variable 'canvas' referenced before
> >> > assignment
> >>
> >> > i haven't posted entire code, because those lines are giving me
> >> > problems. I've searched the web, and all say that I should make var
> >> > global inside function. so I did it, but still nothing...
> >> > using python 2.6.2 and reportlab 2.4
> >> > help plz :)
> >>
> >> The version of mkTable.py you posted is clearly different to the one
> >> in the traceback. You might want to check that.
> >>
> >> Richard.
> >
> > here's full version of mkTable.py (I've cut out all the drawing code
> > for clarity)
> > http://bpaste.net/show/7003/
> >
> > and it's caller :
> > http://bpaste.net/show/7004/
> >
> > and the newest traceback (just generated):
> >
> > Traceback (most recent call last):
> >  File "C:\py\pdf_test.py", line 36, in <module>
> >    mkTable.mkTable()
> >  File "C:\py\mkTable.py", line 38, in mkTable
> >    canvas.setFillColorRGB(0.8,0.8,0.8)
> > UnboundLocalError: local variable 'canvas' referenced before assignment
>
> mkTable.py:
> # -*- coding: utf-8 -*-
> def mkTable():
>    global canvas
> <snip>
>    canvas = canvas.Canvas("K_lista.pdf", pagesize=A4)
>
> The only global variable defined in mkTable.py is the "mkTable"
> function (partly since you don't import anything). So the reference to
> the global variable "canvas" on the right-hand side of this expression
> is completely undefined, resulting in the error you're getting.
>
> In this respect, your code is akin to:
> whatever.py:
> # -*- coding: utf-8 -*-
> def foo():
>    global x
>    x = 2 * x
> foo()
> # This is obviously horribly flawed since x is never given an initial
> value!
> #EOF
>
> More generally, your code uses "canvas" to refer both to the module
> reportlab.pdfgen.canvas and an instance of the class
> reportlab.pdfgen.canvas.Canvas; this is confusing and problematic. I
> suggest you either rename one of them to something distinct (e.g.
> `import reportlab.pdfgen.canvas as pdfcanvas`), or import just the
> class Canvas straight from reportlab.pdfgen.canvas (i.e. `from
> reportlab.pdfgen.canvas import Canvas`).
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>
>
>
> ---------- Forwarded message ----------
> From: Bryan <bryanjugglercryptographer at yahoo.com>
> To: python-list at python.org
> Date: Tue, 8 Jun 2010 02:27:38 -0700 (PDT)
> Subject: Re: Which objects are expanded by double-star ** operator?
> Terry Reedy wrote:
> > Peter Otten wrote:
> >
> > > kkumer wrote:
> >
> > >> I have to merge two dictionaries into one, and in
> > >> a "shallow" way: changing items should be possible
> > >> by operating either on two parents or on a
> > >> new dictionary. I am open to suggestions how
> > >> to do this (values are always numbers, BTW), but
> > >> I tried to do it by creating a dict-like class that just
> > >> forwards all calls to the two parent dicts, see below.
> >
> > >> It works, but one important thing is missing. I
> > >> am not able to expand new dictionary with
> > >> double-star operator ** to use it as a
> > >> set of keyword arguments of a function.
> > >> I googled a bit, but was unable to find what
> > >> property must an object have to be correctly
> > >> treated by **.
> >
> > > The following experiment shows that you only need to implement a keys()
> and
> > > __getitem__() method.
> >
> > > $ cat kw.py
> > > class A(object):
> > >      def keys(self): return list("ab")
> > >      def __getitem__(self, key):
> > >          return 42
> [...]
> > > However, if you have A inherit from dict...
> [...]
> > > it stops working -- probably a side-effect of some optimization.
> > > So if you change your hubDict's base class from dict to object you
> should
> > > get the desired behaviour.
> >
> > In 2.6, the requirement changed from '(subclass of) dictionary' to
> > 'mapping' so this is a bit strange. It sort of looks like a bug. I will
> > test with 3.1 tomorrow (later today, actually).
>
> I get the same bug-like behavior in 3.1. I think Peter is right that
> it's probably a side-effect of an optimization. kkumer seems to have
> completely over-ridden the methods of dict, but if we insert into his
> hubDict with the parent class's method:
>
>  dict.__setitem__(dh, 'c', 3)
>
> Then the **dh argument passes the keyword arg c=3.
>
>
> --
> --Bryan
>
>
>
> ---------- Forwarded message ----------
> From: kkumer <kkumer at that-popular-search-engines-mail.com>
> To: python-list at python.org
> Date: Tue, 8 Jun 2010 09:28:21 +0000 (UTC)
> Subject: Re: Which objects are expanded by double-star ** operator?
> (sorry for posting empty post by accident)
>
> Peter Otten <__peter__ at web.de> wrote:
> > it stops working -- probably a side-effect of some optimization.
> > So if you change your hubDict's base class from dict to object you should
> > get the desired behaviour.
>
> Yes, as already noted, this would require python >=1.6, and I am
> reluctant to upgrade at this point.
>
> What I ended up doing is to simply create a normal dictionary
> and copy all items from my hubDict merged "dictionary" into it
> just for the purpose of this one function call
>
> auxdict = dict((it for it in hubDictinstance.items()))
> f(**auxdict)
>
> Since this happens only few times during runtime it's not a big deal.
>
> Thanks to all for insights.
>
> K.
>
>
>
> ---------- Forwarded message ----------
> From: ch1zra <ch1zra at gmail.com>
> To: python-list at python.org
> Date: Tue, 8 Jun 2010 02:28:22 -0700 (PDT)
> Subject: Re: UnboundLocalError: local variable referenced before assignment
> On Jun 8, 10:59 am, Bryan <bryanjugglercryptograp... at yahoo.com> wrote:
> > ch1zra wrote:
> > > I have following code :
> >
> > > import os, time, re, pyodbc, Image, sys
> > > from datetime import datetime, date, time
> > > from reportlab.lib.pagesizes import A4
> > > from reportlab.lib.units import cm
> > > from reportlab.pdfgen import canvas
> > > from reportlab.pdfbase import pdfmetrics
> > > from reportlab.pdfbase.ttfonts import TTFont
> > > import mkTable
> >
> > > mkTable.mkTable()
> >
> > > and then file mkTable.py located in same directory has :
> >
> > > def mkTable():
> > >     global canvas
> > >     canvas = canvas.Canvas(fname, pagesize=A4)
> > >     ... and so on
> >
> > > this gives me following traceback:
> >
> > > Traceback (most recent call last):
> > >   File "C:\py\pdf_test.py", line 36, in <module>
> > >     mkTable.mkTable()
> > >   File "C:\py\mkTable.py", line 38, in mkTable
> > >     canvas = canvas.Canvas("K_lista.pdf", pagesize=A4)
> > > UnboundLocalError: local variable 'canvas' referenced before
> > > assignment
> >
> > Python doesn't have one global namespace. Each module (file) has its
> > own namespace, which is a Python dict, and 'global' means defined in
> > the containing module's dict. Put the import:
> >
> >   from reportlab.pdfgen import canvas
> >
> > in the mkTable.py file. That brings 'canvas' into the mkTable module's
> > namespace.
> >
> > Python programs commonly import the same module multiple times. Only
> > the first import runs the body of the imported module. Subsequent
> > imports merely bring the names into the importing module's namespace.
> >
> > --
> > --Bryan Olson
>
> thanx so much, it worked.
> I am getting some other problems now, but will use this logic to fix
> it.
> thanx once again :)
>
>
>
> ---------- Forwarded message ----------
> From: Giacomo Boffi <giacomo.boffi at polimi.it>
> To: python-list at python.org
> Date: Tue, 08 Jun 2010 11:41:01 +0200
> Subject: Re: Plotting in batch with no display
> Hans Georg Schaathun <H.Schaathun at surrey.ac.uk> writes:
>
> > :      import matplotlib
> > :      matplotlib.use('agg')
> > :      import pylab
> > :      pylab.plot([1, 3, 5])
> > :      fig = file('foo.png', 'wb')
> > :      pylab.savefig(fig, format='png')
> > :      fig.close()
> >
> > Raster graphics is not good enough
>
> #ig = file('foo.png', 'wb'
> pylab.savefig('foo.png', format='pdf')
>
> that's all
>
>
>
> ---------- Forwarded message ----------
> From: Giacomo Boffi <giacomo.boffi at polimi.it>
> To: python-list at python.org
> Date: Tue, 08 Jun 2010 11:43:41 +0200
> Subject: Re: Plotting in batch with no display
> Giacomo Boffi <giacomo.boffi at polimi.it> writes:
>
> > Hans Georg Schaathun <H.Schaathun at surrey.ac.uk> writes:
> >
> >> :      import matplotlib
> >> :      matplotlib.use('agg')
> >> :      import pylab
> >> :      pylab.plot([1, 3, 5])
> >> :      fig = file('foo.png', 'wb')
> >> :      pylab.savefig(fig, format='png')
> >> :      fig.close()
> >>
> >> Raster graphics is not good enough
> >
> > #ig = file('foo.png', 'wb'
> > pylab.savefig('foo.png', format='pdf')
> >
> > that's all
>
> either
>
> matplotlib.use('cairo.pdf')
> ...
> pylab.savefig('foo.pdf')
>
> --
> le mie sacrosante questioni di principio
>          VS     gli sciocchi puntigli di quel cretino del mio vicino
>
>
>
> ---------- Forwarded message ----------
> From: kkumer <kkumer at this-popular-search-engines-mail.com>
> To: python-list at python.org
> Date: Tue, 8 Jun 2010 09:44:53 +0000 (UTC)
> Subject: Re: Which objects are expanded by double-star ** operator?
> Bryan <bryanjugglercryptographer at yahoo.com> wrote:
> > I get the same bug-like behavior in 3.1. I think Peter is right that
> > it's probably a side-effect of an optimization. kkumer seems to have
> > completely over-ridden the methods of dict, but if we insert into his
> > hubDict with the parent class's method:
> >
> >  dict.__setitem__(dh, 'c', 3)
> >
> > Then the **dh argument passes the keyword arg c=3.
>
> Yes. But problem is that this setting of item
> should also affect one of the two original parent dicts,
> while  your proposal affects only hubDict instance.
> (hubDict is never used to create new items, just to change
> values of existing ones, which belong to one of
> two parents).
>
> K.
>
>
>
> ---------- Forwarded message ----------
> From: Martin <mdekauwe at gmail.com>
> To: python-list at python.org
> Date: Tue, 8 Jun 2010 02:47:19 -0700 (PDT)
> Subject: Re: Reading file bit by bit
> On Jun 7, 9:57 am, "Alfred Bovin" <alf... at bovin.invalid> wrote:
> > Hi all.
> >
> > I'm working on something where I need to read a (binary) file bit by bit
> and
> > do something depending on whether the bit is 0 or 1.
> >
> > Any help on doing the actual file reading is appreciated.
> >
> > Thanks in advance
>
> Hi,
>
> Have you looked at the numpy libraries?
>
> It would be very easy to do...
>
> import numpy as np
>
> f = open("something.bin", "rb")
> data = np.fromfile(f, np.uint8)
> data = np.where(data == 0, data * 5, data)
>
> So in this example I am just saying if data = 0, multiply by 5. This
> saves the need for slow loops as well.
>
> Mart.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
madhuri :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100609/2c572f47/attachment.html>


More information about the Python-list mailing list