Oct-02-2020, 07:29 AM
I know Global's are looked down upon, but in this instance I don't know what else to do. Any in the function at top the print statement works just fine as you will see in the out put. But when same print statement made in another function at the bottom, I just get an empty list.
$ python processing.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Milford\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "processing.py", line 130, in select_track
load_track_to_Handi()
File "processing.py", line 1467, in load_track_to_Handi
print(scratched_list)
NameError: name 'scratched_list' is not defined
[False, False, True, False, False, False] # This is top print statement at moment scratch_list is created. Error is from bottom print statement.
def complete_form(): global scratched_list scratched_list = [] for i, var in enumerate(scratched): scratched_list.append(var.get()) print(scratched_list) #SET UP SCFRATCH WINDOW def get_scratches(horses_name): global namevar1 global scratched scratched =[] scratches = Toplevel() scratches.title("Handifast Version 13.0 Scratch Window") scratches.iconbitmap("c:/guis/racehorse.ico") width_of_window = 500 height_of_window = 700 screen_width= scratches.winfo_screenwidth() screen_height = scratches.winfo_screenheight() x_coordinate = (screen_width/2) - (width_of_window/2) y_coordinate = (screen_height/2) - (height_of_window/2) scratches.geometry("%dx%d+%d+%d" % (width_of_window,height_of_window,x_coordinate,y_coordinate )) my_scratch_label= Label(scratches, text = track_abrev + " " + "Race " + str(race_number), fg= "#473DAD", font = ("sans_serif" , 11, 'bold')).place(x=200, y=5) my_scratch_label1= Label(scratches, text ="Select Horses to Scratch ", fg= "#F50808", font = ("sans_serif" , 14)).place(x=140, y=25) #create_checkboxes FOR SCRATCH WINDOW() namevar1 = StringVar() xx = 55 yy = 50 h = (len(horses_name)) count = 0 for i in range(h): var = BooleanVar() scratched.append(var) button = Checkbutton(scratches, text = str(i+1), variable = var, onvalue = True, offvalue = False).place(x=10, y=yy) #button.deselect() my_scratch_label2= Label(scratches, text =horses_name[count], fg= "black", font = ("sans_serif" , 10, 'bold')).place(x=xx, y=yy) yy+=25 count+=1 process_scratch_button= Button(scratches, text="Process Scratches", bg='#16EF1A', width = 15, command=complete_form) process_scratch_button.place(x=200, y=600) # LOAD TRACK by racenumber TO HANDICAP def load_track_to_Handi(): my_tracklabel= Label(root, text =" ", fg= "#F50808", font = ("sans_serif" , 16)).place(x=380, y=430) read_csvfile_into_array() global track_abrev global race_number global xx global horses_name global hcount global horse_count global horses_info global cnt horse_array = np.arange(2100).reshape(20,105) #if fileFound ==True: horses_info = [] horse_single_race_info = [] horse_saddle = [] horses_name = [] hcount = 0 with open( 'C://guis/f_racefiles/'+ track_abrev + '.csv' , 'r') as race_file: df = pd.read_csv(race_file, header =None) race_data = df[(df[1] == race_number)] #print(race_data[18]) #print(df[18]) xx = df.iloc[ :, 0: 105 ].values #race_data.set_index(18, inplace = True) #print(race_data.loc[0, 18]) #READ FILE AND PULL OUT HORSES AND HORSE INFO FOR SELECTED RACE AND TRACK #for cnt in range(0,len(xx)): for cnt in range(0,len(xx)): if int(xx[cnt][1])== int(race_number): horses_name.append(xx[cnt][18]) get_scratches(horses_name) #for i in range(0,len(race_data)): #if scratched_list[i] == 'True': #race_data.drop(i) print(scratched_list) #Here is where the error is thrown. says not defined but scratched_list is a global.Here is result from the two print statements.
$ python processing.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Milford\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "processing.py", line 130, in select_track
load_track_to_Handi()
File "processing.py", line 1467, in load_track_to_Handi
print(scratched_list)
NameError: name 'scratched_list' is not defined
[False, False, True, False, False, False] # This is top print statement at moment scratch_list is created. Error is from bottom print statement.