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

> I get the same bug-like behavior in 3.1. I think Peter is right that<br>
> it's probably a side-effect of an optimization. kkumer seems to have<br>
> completely over-ridden the methods of dict, but if we insert into his<br>
> hubDict with the parent class's method:<br>
><br>
>  dict.__setitem__(dh, 'c', 3)<br>
><br>
> Then the **dh argument passes the keyword arg c=3.<br>
<br>
Yes. But problem is that this setting of item<br>
should also affect one of the two original parent dicts,<br>
while  your proposal affects only hubDict instance.<br>
(hubDict is never used to create new items, just to change<br>
values of existing ones, which belong to one of<br>
two parents).<br>
<br>
K.<br>
<br>
<br><br>---------- Forwarded message ----------<br>From: Martin <<a href="mailto:mdekauwe@gmail.com">mdekauwe@gmail.com</a>><br>To: <a href="mailto:python-list@python.org">python-list@python.org</a><br>Date: Tue, 8 Jun 2010 02:47:19 -0700 (PDT)<br>
Subject: Re: Reading file bit by bit<br>On Jun 7, 9:57 am, "Alfred Bovin" <alf...@bovin.invalid> wrote:<br>
> Hi all.<br>
><br>
> I'm working on something where I need to read a (binary) file bit by bit and<br>
> do something depending on whether the bit is 0 or 1.<br>
><br>
> Any help on doing the actual file reading is appreciated.<br>
><br>
> Thanks in advance<br>
<br>
Hi,<br>
<br>
Have you looked at the numpy libraries?<br>
<br>
It would be very easy to do...<br>
<br>
import numpy as np<br>
<br>
f = open("something.bin", "rb")<br>
data = np.fromfile(f, np.uint8)<br>
data = np.where(data == 0, data * 5, data)<br>
<br>
So in this example I am just saying if data = 0, multiply by 5. This<br>
saves the need for slow loops as well.<br>
<br>
Mart.<br>
<br>
<br>--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br><br clear="all"><br>-- <br>madhuri :)<br><br>