0

I have a student that is working on a task and trying to use global variables within a couple functions across different files. I have had to include excerpts of the files. The first file is the main one and when you click the results button in that program (from the main window it creates) it should call the other file and pass a variable. But, he gets the following error...

Results.py", line 44, in GiveResults if row[0] == sname: NameError: name 'sname' is not defined

I'm hoping that someone with much better ability and knowledge might be able to point us in the right direction to remedy this. If there is a better way to share the code on here then please also let me know.

Thanks,

Scott

'Solution.py'

#This imports the tkinter module of python 
from tkinter import *

#This imports the other windows for use later
import Results, New_User, Edit_User

#This part forms the main window and controls things such as size, colour, and message
main = Tk()
main.title('Hello there')
main.geometry("1000x600")
main['background']='light blue'

#This creates a frame for use later
window = Frame(main).pack()

#Defines a function that when called will convert whatever is in the textboxes to variables
def retrieve():
    global sname
    sname = selectedname.get()
    global sboss
    sboss = selectedname.get()

'Results.py'

from tkinter import *

#This is defining the function that the first window is calling upon
def GiveResults():

    #This is defining the variables as globe for use across windows (Although it isnt working)
    global sname
    global sboss
    global inputt
    
    #Defines a quit function to close the window when called
    def quit():
        ResultsGiven.destroy()
    
    #This is making the window
    ResultsGiven = Tk()
    ResultsGiven.title('You did the program')
    ResultsGiven.geometry("600x400")
    ResultsGiven['background']='light blue'
    
    #Creating a frame
    windowr = Frame(ResultsGiven, bg = 'light blue')
    
    #Creating a title
    titlefont = ('papyrus', 30) 
    title = Label(windowr, text='Results', font=titlefont)
    title.config(height=1, width=400) 
    title.pack(side=TOP, fill = 'x')
    
    #Creating a canvas
    canvasr = Canvas(windowr, width = 400, height = 400, background = 'light blue')
    canvasr.pack()
    
    #This is importing the csv module of python
    import csv
            
    #This is opening the csv file created when a new user is made
    #It is then creating a reader to check the first column for the name entered in the main window
    #When it finds a match it moves along that row to find the class
    #(Unfinished)
    with open("userbase.csv") as f:
        for row in csv.reader(f):
            if row[0] == sname:
                sclass = str(column[2])```
Scott
  • 3
  • 2
  • first do this in `Results.py` add on the top `from Solutions import *` then try `row[0] == str(sname)` – Bhaskar pal Aug 31 '20 at 05:29
  • 1
    1) The `sname` variable is not initialized, so try initializing it outside of the function, maybe as `None`. 2) Trying using `Results.sname` in `Solutions.py`, I can't tell what you want but if you want it to be used from solutions in results, try `Solutions.sname` – M Z Aug 31 '20 at 05:30
  • 1
    **all of this would be so much easier if you ignored globals and just returned the value from the function directly** – M Z Aug 31 '20 at 05:32
  • Does this answer your question? [Using global variables between files?](https://stackoverflow.com/questions/13034496/using-global-variables-between-files) – M Z Aug 31 '20 at 05:33
  • 1
    "global" is a misnomer - it refers to the module's namespace, not something available to all modules. `global sname` in one module is `Solution.sname` while in the other its `Results.sname`. There is no truly global variable in python. – tdelaney Aug 31 '20 at 05:36

0 Answers0