全局变量在此Python代码中不起作用

问题描述

我有一个学生正在从事一项任务,并试图在跨不同文件的几个函数中使用全局变量。我必须包括这些文件的摘录。第一个文件是主文件,当您在该程序中单击结果按钮(从它创建的主窗口)时,它应调用一个文件并传递一个变量。但是,他得到了以下错误...

Results.py”,如果row [0] == sname:NameError:未定义名称“ sname”,则在GiveResults中的第44行

我希望具有更好的能力和知识的人能够为我们指出正确的方向来对此进行补救。如果有更好的方法可以在此处共享代码,请告诉我。

谢谢

斯科特

'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])```

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)