Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global not working why?
#1
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.

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.
Reply


Messages In This Thread
Global not working why? - by Milfredo - Oct-02-2020, 07:29 AM
RE: Global not working why? - by ibreeden - Oct-02-2020, 08:08 AM
RE: Global not working why? - by Larz60+ - Oct-02-2020, 10:33 AM
RE: Global not working why? - by scidam - Oct-02-2020, 11:09 AM
RE: Global not working why? - by Milfredo - Oct-03-2020, 01:28 AM
RE: Global not working why? - by scidam - Oct-03-2020, 01:46 AM
RE: Global not working why? - by Milfredo - Oct-04-2020, 06:44 AM
RE: Global not working why? - by bowlofred - Oct-04-2020, 07:21 AM
RE: Global not working why? - by ndc85430 - Oct-04-2020, 10:48 AM
RE: Global not working why? - by hshivaraj - Oct-04-2020, 12:30 PM
RE: Global not working why? - by deanhystad - Oct-04-2020, 12:44 PM
RE: Global not working why? - by Milfredo - Oct-05-2020, 11:45 PM
RE: Global not working why? - by Larz60+ - Oct-06-2020, 12:29 AM
RE: Global not working why? - by Milfredo - Oct-06-2020, 02:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Global variables not working hobbyist 9 4,808 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global variable does not seem to be global. Columbo 6 3,767 Jul-15-2019, 11:00 PM
Last Post: Columbo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020