[Tutor] FW: NameError: name 'pics' is not defined
Alan Gauld
alan.gauld at yahoo.co.uk
Thu Apr 15 18:56:17 EDT 2021
On 15/04/2021 19:49, derek wrote:
> from tkinter import filedialog
>
> global pics
This does not do what you think it does.
In fact at this point in the code I don't think
it does anything! Certainly nothing useful.
global is used inside a function to resolve name
conflicts. To tell Python that a given name should
refer to the global(ie module) scope and not be
created as a local variable inside the function.
It does not create the variable.
> # definitions
> def openFile():
> pics = filedialog.askopenfilename()
And this pics is local to the function because
you did not declare it global inside the function.
You need to move the global line inside this function.
> win = tk.Tk()
> button = Button(text=TitleOfOFD, command=openFile)
You need to tell the Button who its parent is otherwise
it will be an orphan control. ie you need win as the first
argument.
> canvas = Canvas(win, width=300, height=300)
> canvas.create_image(200, 200, anchor=NW, image=pics)
And here you refer to a non existent pics. So presumably,
this is where the error comes from.
When posting always include the full error trace,
it contains a lot of useful details
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list