0

So I'm currently doing a project that is a random generator that reads on an excel file and select a data randomly but as I develop my code I get the following error:

line 19, in RandomG
    L2 = Label(r2, text=edata).grid(row=3, column=1).grid(rowspawn=3, sticky=S)
NameError: name 'edata' is not defined

I have been told once to use "global" so the variables applies to all functions but it still gives me the error.

Here is my code:

from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename
import xlrd
import random
import os

creds = 'Python file.txt' # This just sets the variable creds to 'tempfile.temp'

def RandomG():
    global r2
    r.destroy()
    rootA.destroy()
    r2 = Tk()
    r2.title('Random Generator')
    r2.geometry('350x300')
    r2.resizable(False, False)
    L1 = Label(r2, text='Welcome to My Random Generator!!').grid(row=1, column=1)
    L2 = Label(r2, text= edata).grid(row=3, column=1).grid(rowspawn=3, sticky=S)
    L2.config(width=200)
    B1 = Button(r2, text="Browse", command=Directory)
    B1.grid(rowspan=3, sticky=S)
    B2 = Button(r2, text="Randomize", command=Generator)
    B2.grid(rowspan=3, sticky=S)
    r2.mainloop

def Directory():
    global filename
    filename = askopenfilename(filetypes=(("Excel Files", ".xlsx"),("All files","*.*")))
    pathlabel = Label(r2)
    pathlabel.config(text=(filename))
    pathlabel.grid(columnspan=2)
    pathlabel.pack()

def Generator():
    global edata
    global sheet
    global workbook
    with open(filename)as a:
        filelocation = (filename)
        workbook = xlrd.open_workbook(filelocation)
        sheet = workbook.sheet_by_index(0)
        for col in range(sheet.nrows):
            edata = print[sheet.pop(sheet.random.cell.value(0, col))]
James Z
  • 12,209
  • 10
  • 24
  • 44
Francis
  • 29
  • 5
  • 2
    *Also I would kindly accept any suggestions for improvement of my current code thank you :)*.... Use [Code Review](https://codereview.stackexchange.com) instead for this purpose.. – letsintegreat May 09 '18 at 11:04
  • None of your functions are ever executed. Please post a [mcve]. – Aran-Fey May 09 '18 at 11:24
  • I'd recommend edit and removing a lot of the code that isn't directly related to the problem at hand (namely "edata"). @HarshitSeksaria is also correct that you'll likely have better feedback on Code Review than here. – Tom May 09 '18 at 12:36
  • Thank you guys so much for you time ^_^ – Francis May 09 '18 at 12:58

2 Answers2

1

this is not how the global keyword works... global just tells your function to look up the variable in the global namespace (instead of the local namespace of your function).

this would work:

N = None

def f():
    global N
    print(N)

without defining N first (or setting it inside your function) it will not.

this would also work:

def f():
    global N
    N = 5
    print(N)

(and it would spill N to your global namespace).

in any case: you have to set N (or edata in your case) before you try to read its value.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
0

This means that edata doesn't have a value. It needs to be defined before calling it.

  • edata = print[sheet.pop(sheet.random.cell.value(0, col))] – Francis May 09 '18 at 11:09
  • Yes I see. You are trying to call it from a different function. –  May 09 '18 at 11:13
  • Define it before you are calling it. check this link out: https://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa –  May 09 '18 at 11:14
  • thats why theres a global to use that variable in another function – Francis May 09 '18 at 11:14
  • Define it before calling it as in bring the function it is being defined in before the function you are calling it. Maybe I should have been more clear –  May 09 '18 at 11:19
  • Dude theres already globals Well thanks for the help anyways: – Francis May 09 '18 at 11:21
  • Your using the globals incorrectly but check out the link i sent to use them properly. –  May 09 '18 at 11:22